XmlReader - Self-closing element does not fire a EndElement event?

孤人 提交于 2019-11-26 14:33:57

问题


I am using XmlReader in .NET to parse an XML file using a loop:

while (xml.Read()) {
   switch xml.NodeType {
     case XmlNodeType.Element:
      //Do something
     case XmlNodeType.Text:
      //Do something
     case XmlNodeType.EndElement:  
      //Do something
   }
}

I was wondering if it was normal that the following XML code does not produce some EndElement nodes? Please note the missing space before the /> but I don't think that's the problem.

<date month="November" year="2001"/>
<zone name="xml"/>

Is there a different NodeType or property to indicate a self-closing element?


回答1:


No, you check it by looking at XmlReader.IsEmptyElement.

In the docs for that property:

A corresponding EndElement node is not generated for empty elements.



来源:https://stackoverflow.com/questions/241336/xmlreader-self-closing-element-does-not-fire-a-endelement-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!