我正在尝试使用 XmlReader 解析文件并使用 reader.GetAttribute("atrribute_name") 将每个元素的属性设置为变量,但是这些元素可能实际存在也可能不存在该属性,因此某些元素给了我一个错误...我本来希望当属性不存在时它只会返回 null ,但它会引发错误。
这是完整的例外:
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
这是xml内容:
<?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>
不透明度可能存在也可能不存在于每一层中,以及其他几个属性。