我已经设置了xmlReader并且可以检查某些元素,但是我找不到检查结束元素的方法,假设我想要为</perls>
标记添加另一个case语句打开一个,我怎么能这样做?我确信这样的标签不是自动关闭的。
using (XmlReader reader = XmlReader.Create("perls.xml"))
{
while (reader.Read())
{
// Only detect start elements.
if (reader.IsStartElement())
{
// Get element name and switch on it.
switch (reader.Name)
{
case "perls":
// Detect this element.
Console.WriteLine("Start <perls> element.");
break;
case "article":
// Detect this article element.
Console.WriteLine("Start <article> element.");
// Search for the attribute name on this current node.
string attribute = reader["name"];
if (attribute != null)
{
Console.WriteLine(" Has attribute name: " + attribute);
}
// Next read will contain text.
if (reader.Read())
{
Console.WriteLine(" Text node: " + reader.Value.Trim());
}
break;
}
}
}
}
答案 0 :(得分:6)
如何简单地检查NodeType
是否等于XmlNodeType.EndElement
,如:
if (reader.NodeType == XmlNodeType.EndElement)
{
....
}
供参考:http://www.codingeverything.com/2013/05/extending-xmlreader-class-isendelement.html