Tutorial :XmlReader DTD Validation Error on an Attribute


I am trying to use XmlReader to parse a file and set each element's attributes to variables using reader.GetAttribute("atrribute_name"), but the elements may or may not actually have that attribute present, so some elements give me an error...I would have expected it would just return null when the attribute is not present, but instead it throws errors.

Here's the full exception:

System.Xml.Schema.XmlSchemaException: The 'opacity' attribute is not declared.     at System.Xml.XmlValidatingReaderImpl.InternalValidationCallback(Object sender, ValidationEventArgs e)     at System.Xml.Schema.BaseValidator.SendValidationEvent(XmlSchemaException e, XmlSeverityType severity)     at System.Xml.Schema.BaseValidator.SendValidationEvent(XmlSchemaException e)     at System.Xml.Schema.DtdValidator.ValidateStartElement()     at System.Xml.Schema.DtdValidator.ProcessElement()     at System.Xml.Schema.DtdValidator.ValidateElement()     at System.Xml.Schema.DtdValidator.Validate()     at System.Xml.XmlValidatingReaderImpl.ProcessCoreReaderEvent()     at System.Xml.XmlValidatingReaderImpl.Read()     at Squared.Tiled.Map.Load(String filename, ContentManager content) in C:\Users\Stephen\Documents\Visual Studio 2008\Projects\Tiled\Tiled.cs:line 650  

and here's the xml content:

<?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd">  <map version="1.0" orientation="orthogonal" width="32" height="32" tilewidth="32" tileheight="32">   <tileset name="Untitled" firstgid="1" tilewidth="32" tileheight="32">    <image source="tiles.png"/>   </tileset>  <layer name="" width="32" height="32" opacity="0.72">    <data encoding="base64" compression="gzip">     H4sIAAAAAAAAAO3DAQkAAAwEofv+pddjKLhqqqqq6usHHB1pSAAQAAA=    </data>   </layer>   <layer name="Layer 1" width="32" height="32">    <properties>     <property name="layermeta" value="layervalue"/>    </properties>    <data encoding="base64" compression="gzip">     H4sIAAAAAAAAAO2UUQrAMAhD+7P7X3nsbwydibrJRh6UQloatbZrzbMZI9KZc5g4EB3dx9Lhn8m707+C5cP2ReX+p7ByuK55WlQz1P9u9vZFOkrVJ6pXxR/pM/TcjD+in9e/1vsHXtzV/Cfqn+GJ+2f9u+qfgX3Pf/v/hRBCiLfZAUDgXx4AEAAA    </data>   </layer>  </map>  

opacity may or may not be present in each layer, along with several other attributes.


Yes. Read the documentation for XmlReader. You'll find it has many methods and properties.

In particular, you'll find the Item property, the HasAttributes property, and the AttributeCount property.


Are you sure that where you have "attribute_name" in your example you are always passing a valid string? Seems to me that your error could be because you're passing null to GetAttribute.


Apparently the issue wasn't xml related at all; the GetAttribute() calls on attributes that weren't present were returning null...but it was passing into float.Parse(), which doesn't work. The compiler kept telling me it was XmlSchema related, so I was looking in the wrong places. >.>

That bug was such a pain to debug.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »