-1

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Stephen Belanger
  • 6,251
  • 11
  • 45
  • 49
  • Please be more specific - "or something like that" just leaves us guessing. Provide the exact error and some code so we can actually identify the issue rather than guess. Thanks. – Jeff Yates Jul 14 '09 at 21:30
  • Stephen, what you need to do is post the XML you're trying to parse, and post the full exception (it's not an "error"). Catch the exception, then post ex.ToString(). – John Saunders Jul 14 '09 at 21:56
  • 1
    Stephen, you may notice how little by little, more information is coming out. You might want to take a few minutes to get ahead of that process. My next question is going to be about the code that reads the XML (and that sets up the validation that's failing). After you post that code, take a little while to figure out what _else_ you're not telling us. – John Saunders Jul 14 '09 at 22:09
  • One small thing that probably doesn't matter - try specifying a non-empty name for the first layer. – John Saunders Jul 14 '09 at 22:19
  • doesn't do anything. before trying the HasAttributes thing this worked fine if I commented out the line, Opacity = st.GetAttribute("opacity"); – Stephen Belanger Jul 14 '09 at 22:24

3 Answers3

1

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
0

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.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
  • That's not it. The attribute collecting code is passed over several times, as there is several of the same element. The first few times it passes over fine because all the attributes I'm trying to assign to variables are present. But then it throws and error when it gets to one of them that doesn't have that attribute. I can't use AttributeCount because there is several attributes and all of them are optional, so it can't be predicted. – Stephen Belanger Jul 14 '09 at 21:33
0

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.

Stephen Belanger
  • 6,251
  • 11
  • 45
  • 49
  • Yeah, but that doesn't solve your XmlSchemaException problem, does it? – John Saunders Jul 14 '09 at 23:21
  • Yes, it does actually--all I did was this, and it worked: string tempopacity = reader.GetAttribute("opacity"); if (tempopacity != null) { result.Opacity = float.Parse(tempopacity); } – Stephen Belanger Jul 14 '09 at 23:28