我看你已經發現了一個相當乾淨的解決方案,而我是編碼一個例子。
也許這可以幫助你多一點:
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;
}
}
問候, 尼科
的XPath沒有LINQ –
有限公司以.NET 3.5 –
因爲這是用於訓練目的。我知道LINQ是easyer,但我必須學習沒有任何LINQ的XPath。 –