【问题标题】:C# parse XML FileC# 解析 XML 文件
【发布时间】:2012-01-01 21:49:58
【问题描述】:

我在用 C# 解析我的 XML 文件(RSS 提要)时遇到问题。 我只想读出“条目”条目(根父级 - “提要” - 不相关)。 除了“状态”部分外,所有“条目”条目几乎都是偶数。有些条目没有那个条目。

所以我只想读出以下内容: “入口”节点:

  1. 更新
  2. 过期
  3. 标题
  4. 总结
  5. 状态(如果存在)

有什么建议吗? 非常感谢。

<?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;
            }
        }
    }

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

【问题讨论】:

  • 你试过什么?你哪里有困难?您使用的是哪个版本的 .NET?
  • 我使用的是 .net 4.0。如何在评论中发布格式化的代码段?
  • 不要在 cmets 中发布格式化代码 - 相反,编辑您的问题并添加详细信息。
  • 请不要使用ArrayList - 请改用List&lt;string&gt;。这样,您至少具有类型安全性。
  • 嗯好的,我会记住这个^^。

标签: c# xml parsing .net-4.0


【解决方案1】:

我相信直接解析 XML 的最简单方法是使用 LINQ-TO-XML。您可以找到更多信息here

【讨论】:

【解决方案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 表达式中的意思是,您要选择具有特定名称的所有节点,无论它们在哪里。

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

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2016-04-15
    • 1970-01-01
    • 2021-03-27
    • 2013-05-29
    • 2015-03-14
    • 1970-01-01
    相关资源
    最近更新 更多