简体   繁体   English

在C#中解析XML文档

[英]Parsing an XML Document in C#

I'm using an xmlreader to parse a valid xml file. 我正在使用xmlreader解析有效的xml文件。 I start reading and have an if statement like so: 我开始阅读,并有一个if语句,如下所示:

if (reader.IsStartElement())
{
     // Code needed here

How do I grab just the name of that start element? 如何仅获取该起始元素的名称?

You can just check the value of reader.Name 您可以只检查reader.Name的值

if (reader.IsStartElement())
{
     if (reader.Name == "settings")
     {
          //do stuff

But as the others suggested, you might want to read more documentation about XmlReader, and perhaps even think about simply parsing the full xml document with XDocument etc. 但是,正如其他人所建议的那样,您可能想阅读有关XmlReader的更多文档,甚至可能考虑只用XDocument等解析完整的xml文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM