C# 解析 XML 文件。

3
我有一个问题,需要在C#中解析我的XML文件(RSS Feed)。我只想读取“entry”条目(根父项“feed”不相关)。所有的“entry”条目几乎都一样,除了“state”部分。有些条目没有该条目。
因此,我只想读取以下内容: “entry”节点:
1. updated 2. expires 3. title 4. summary 5. state(如果存在)
有什么建议吗? 非常感谢。
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
   <updated>2011-01-01T00:00:00+0100</updated>
   <link href="http://www.domain.com" rel="self"/>
   <author>
      <name>Mr X</name>
      <email>Mr_X@domain.com</email>
   </author>
   <title>Some infos....</title>
   <id>domain.com</id>
<entry>
   <updated>2011-01-01T00:00:00Z</updated>
   <expires>2011-01-02T00:00:00Z</expires>
   <title>My first Title</title>
   <id>First ID</id>
  <link type="text/html" rel="alternate"
        href="http://domain.com/firstElement"></link>
   <summary>My first important summary</summary>
   <rights>domain.com</rights>
   <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
         <div>
            <img alt="second" width="32"
                 src="http://domain.com/firstElement.png"/>
         </div>
      </div>
   </content>
</entry>
<entry>
  <updated>2011-01-01T00:00:00Z</updated>
  <expires>2011-01-02T00:00:00Z</expires>
  <title>My second Title</title>
  <state>active</state>
  <id>Second ID</id>
  <link type="text/html" rel="alternate"
        href="http://domain.com/secondElement"></link>
  <summary>My second important summary</summary>
  <rights>domain.com</rights>
  <content type="xhtml">
    <div xmlns="http://www.w3.org/1999/xhtml">
      <div>
        <img alt="second" width="32"
              src="http://domain.com/secondElement.png"/>
      </div>
    </div>
  </content>
  </entry>
</feed>{<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
   <updated>2011-01-01T00:00:00+0100</updated>
   <link href="http://www.domain.com" rel="self"/>
   <author>
      <name>Mr X</name>
      <email>Mr_X@domain.com</email>
   </author>
   <title>Some infos....</title>
   <id>domain.com</id>
<entry>
   <updated>2011-01-01T00:00:00Z</updated>
   <expires>2011-01-02T00:00:00Z</expires>
   <title>My first Title</title>
   <id>First ID</id>
  <link type="text/html" rel="alternate"
        href="http://domain.com/firstElement"></link>
   <summary>My first important summary</summary>
   <rights>domain.com</rights>
   <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
         <div>
            <img alt="second" width="32"
                 src="http://domain.com/firstElement.png"/>
         </div>
      </div>
   </content>
</entry>
<entry>
  <updated>2011-01-01T00:00:00Z</updated>
  <expires>2011-01-02T00:00:00Z</expires>
  <title>My second Title</title>
  <state>active</state>
  <id>Second ID</id>
  <link type="text/html" rel="alternate"
        href="http://domain.com/secondElement"></link>
  <summary>My second important summary</summary>
  <rights>domain.com</rights>
  <content type="xhtml">
    <div xmlns="http://www.w3.org/1999/xhtml">
      <div>
        <img alt="second" width="32"
              src="http://domain.com/secondElement.png"/>
      </div>
    </div>
  </content>
  </entry>
</feed>

我目前的C#代码:

public void ParseXML(XmlDocument xmlFile)
    {
        ArrayList updated = new ArrayList();
        ArrayList expires = new ArrayList();
        ArrayList title = new ArrayList();
        ArrayList summary = new ArrayList();
        ArrayList state = new ArrayList();

        ObservableCollection<TrafficInformation> trafInfo = new ObservableCollection<TrafficInformation>();
        myCollection = trafInfo;
        XmlNodeReader reader = new XmlNodeReader(xmlFile);

        StringBuilder output = new StringBuilder();

        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    if(reader.Name == "updated")
                    {
                        updated.Add(reader.ReadString());
                    }

                    if (reader.Name == "expires")
                    {
                        expires.Add(reader.ReadString());
                    }

                    if (reader.Name == "title")
                    {
                        title.Add(reader.ReadString());
                    }

                    if (reader.Name == "summary")
                    {
                        summary.Add(reader.ReadString());
                    }

                    if (reader.Name == "state")
                    {
                        state.Add(reader.ReadString());
                    }

                    break;
            }
        }
    }

在这种情况下,如果状态不存在,我就没有数据之间的关系。

3
你尝试过什么?你遇到了哪些困难?你使用的是哪个版本的.NET? - Oded
我正在使用 .net 4.0。我如何在评论中发布格式化的代码段? - user1011394
请勿在评论中发布格式化代码 - 相反,请编辑您的问题并添加详细信息。 - Oded
2
请不要使用ArrayList - 改用List<string>。这样,你至少拥有类型安全性。 - Oded
嗯好的,我会记住这个的^^。 - user1011394
2个回答

8

我认为直接使用LINQ-TO-XML来解析XML是最简单的方法。你可以在这里找到更多信息(链接)


我发现这个链接对于开始使用LINQ to XML更有用:http://www.dotnetcurry.com/showarticle.aspx?ID=564 - rdans

2
您可以使用XPath表达式来实现。下面是关于控制台应用程序的完整示例 - 因为您使用了xlmns命名空间,所以需要对ParseXML方法进行一些修改。
using System;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load("XMLFile1.xml");
            XmlNamespaceManager xmlnm = new XmlNamespaceManager(xmlDocument.NameTable);
            xmlnm.AddNamespace("ns", "http://www.w3.org/2005/Atom");

            ParseXML(xmlDocument, xmlnm);

            Console.WriteLine("\n---XML parsed---");
            Console.ReadKey();
        }

        public static void ParseXML(XmlDocument xmlFile, XmlNamespaceManager xmlnm)
        {
            XmlNodeList nodes = xmlFile.SelectNodes("//ns:updated | //ns:expires | //ns:title | //ns:summary | //ns:state", xmlnm);

            foreach (XmlNode node in nodes)
            {
                Console.WriteLine(node.Name + " = " + node.InnerXml);
            }
        }
    }
}

//在XPath表达式中表示,您想选择具有特定名称的所有节点,无论它们在哪里。

如果您只想搜索<entry></entry>元素,则可以使用以下内容:
"//ns:entry/ns:updated | //ns:entry/ns:expires | //ns:entry/ns:title | //ns:entry/ns:summary | //ns:entry/ns:state"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接