C# XmlReader - 自关闭元素不会触发 EndElement 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/241336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:27:49  来源:igfitidea点击:

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

c#.netxml.net-2.0xmlreader

提问by Vincent

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

我在 .NET 中使用 XmlReader 来使用循环解析 XML 文件:

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.

我想知道以下 XML 代码不生成某些 EndElement 节点是否正常?请注意 /> 之前缺少的空格,但我认为这不是问题所在。

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

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

是否有不同的 NodeType 或属性来指示自闭合元素?

采纳答案by Jon Skeet

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

不,您通过查看XmlReader.IsEmptyElement 来检查它。

In the docs for that property:

在该属性的文档中:

A corresponding EndElement node is not generated for empty elements.

不会为空元素生成相应的 EndElement 节点。