2011-07-04 26 views
3

我有一個字符串以下XML:如何使用C#中的XPath將XML解析爲IList <BusinessObject>?

<RootElement> 
    <Data> 
     <Row> 
      <Id>1</Id> 
      <Name>Foo</Name> 
     </Row> 
     <Row> 
      <Id>2</Id> 
      <Name>Bar</Name> 
     </Row> 
    </Data> 
</RootElement> 

及以下等級:

public class BusinessObject 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

如何可以解析在使用XPath行元素爲IList所有的數據?

我需要學習這個培訓。

感謝您的回答。

+0

的XPath沒有LINQ –

+0

有限公司以.NET 3.5 –

+0

因爲這是用於訓練目的。我知道LINQ是easyer,但我必須學習沒有任何LINQ的XPath。 –

回答

4
IEnumerable<BusinessObject> ParseWithXPath(string xml) 
{ 
    XmlDocument doc = new XmlDocument(); 
    doc.LoadXml(xml); 
    foreach (XmlNode node in doc.DocumentElement.SelectNodes("Data/Row")) // XPath query 
    { 
     yield return new BusinessObject 
     { 
      Id = Int32.Parse(node.SelectSingleNode("Id").InnerText), 
      Name = node.SelectSingleNode("Name").InnerText 
     }; 
    } 
} 

用法:

IEnumerable<BusinessObject> seq = ParseWithXPath(xml); // .NET 2.0+ 

IList<BusinessObject> list = new List<BusinessObject>(seq); // .NET 2.0+ 
+0

@Freeman:立即嘗試。爲我工作。 – abatishchev

+0

現在它編譯,但它不會返回任何東西。我並不熟悉產量如何工作。該集合是否需要實例化? –

+0

@Freeman:這是本機'IEnumerable '返回。它返回一些東西;否則 - 空序列。我測試了您發佈的XML,並且代碼返回了2個對象。 http://pastebin.com/BzsgAX2K – abatishchev

0
IEnumerable<BusinessObject> busObjects = from item in doc.Descendants("Row") 
             select new BusinessObject((int)item.Element("Id"), (string)item.Element("Name")); 

你可以嘗試類似上面的東西。您的BusinessObject構造函數應該採用這兩個參數。 BusinessObject(int id, string name)

請看下面的link例如在Linq to XML上。

+0

這就是LINQ to XML,而不是XPath – abatishchev

+0

@abatishchev,這的確如此。 – Jethro

3

我看你已經發現了一個相當乾淨的解決方案,而我是編碼一個例子。

也許這可以幫助你多一點:

internal static class XMLToListWithXPathExample 
{ 
    static XmlDocument xmlDocument; 
    static List<BusinessObject> listBusinessObjects; 
    static string sXpathStatement = "/Data/Row"; 

    static void LoadXMLData(string p_sXMLDocumentPath) 
    { 
     xmlDocument = new XmlDocument(); // setup the XmlDocument 
     xmlDocument.Load(p_sXMLDocumentPath); // load the Xml data 
    } 

    static void XMLDocumentToList() 
    { 
     listBusinessObjects = new List<BusinessObject>(); // setup the list 
     foreach (XmlNode xmlNode in xmlDocument.SelectNodes(sXpathStatement)) // loop through each node 
     { 
      listBusinessObjects.Add(// and add it to the list 
       new BusinessObject(
        int.Parse(xmlNode.SelectSingleNode("Id").InnerText), // select the Id node 
        xmlNode.SelectSingleNode("Name").InnerText)); // select the Name node 
     } 

    } 
} 

public class BusinessObject 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 

    // constructor 
    public BusinessObject(int p_iID, string p_sName) 
    { 
     Id = p_iID; 
     Name = p_sName; 
    } 

} 

問候, 尼科

+0

將「.DocumentElement」添加到「xmlDocument.DocumentElement.SelectNodes」並將sXpathStatement修改爲「Data/Row」後,它將運行並正確返回數據。我更喜歡你的解決方案,因爲它不需要收益回報(最近做了相當多的研究),但我選擇了abatishchev答案作爲正確的原因,它不需要對BusinessClass進行修改。感謝您的回覆併爲您的時間+1了 –

+0

非常歡迎,謝謝! –