属性上的 XmlReader DTD 验证错误

发布于 2024-07-27 04:17:47 字数 2118 浏览 3 评论 0原文

我正在尝试使用 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>

每层中可能存在也可能不存在不透明度以及其他几个属性。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云淡风轻 2024-08-03 04:17:47

是的。 阅读 XmlReader 的文档。 您会发现它有许多方法和< a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlreader_properties.aspx" rel="nofollow noreferrer">属性。

特别是,您会发现 Item 属性HasAttributes 属性,以及 AttributeCount 属性

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.

很酷又爱笑 2024-08-03 04:17:47

您确定示例中具有“attribute_name”的位置始终传递有效字符串吗? 在我看来,您的错误可能是因为您将 null 传递给 GetAttribute。

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.

赠意 2024-08-03 04:17:47

显然这个问题根本与 xml 无关; GetAttribute() 调用不存在的属性返回 null...但它传递到 float.Parse(),这不起作用。 编译器一直告诉我它与 XmlSchema 相关,所以我找错了地方。 >。>

这个 bug 调试起来非常痛苦。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文