2011-11-19 129 views
3

我有一個問題來解析我的XML文件(RSS源)在C#中。 我只想讀出「entry」條目(根父 - 「feed」 - 不相關)。 所有「條目」條目幾乎均勻,除了「狀態」部分。有些條目沒有該條目。C#解析XML文件

所以我只想讀出以下內容: 「項」 節點:

  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>[email protected]</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>[email protected]</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

+0

我正在使用.net 4.0。如何在評論中發佈格式化的代碼段? – user1011394

+0

不要在註釋中發佈格式化的代碼 - 相反,編輯您的問題並添加細節。 – Oded

回答

2

您可以使用XPath表達式。下面是console-appliactaion的完整示例 - 當您使用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"

8

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

+0

我發現這對linq to xml入門更有用:http://www.dotnetcurry.com/showarticle.aspx?ID=564 – rdans