2012-02-23 19 views

回答

0

對於.NET 2.0解決方案請參閱下面的例子:

我輸入文件名爲Books.xml和它的存儲INT我的樣本項目的Properties.Resources:

<?xml version="1.0" encoding="UTF-8" ?> 
<books> 
<book Title="Rage of angels" Author="Sidney Sheldon">Book 1</book> 
<book Title="If tomorrow comes" Author="Sidney Sheldon">Book 2</book> 
<book Title="Stranger in the mirror" Author="Sidney Sheldon">Book 3</book> 
<book Title="A studyin scarlet" Author="Sir Arthur Conan Doyle">Book 4</book> 
</books> 

以下是處理xml文檔並將Title屬性的值寫入Visual Studio中的輸出窗口的實際代碼。簡單地修改代碼以滿足您的XML文件,你是好去:

protected void Page_Load(object sender, EventArgs e) 
{ 
    XmlDocument document = new XmlDocument(); 
    document.LoadXml(Properties.Resources.books.ToString()); 
    ProcessXml(document,"/books/book", "book", "Title"); 
} 

private void ProcessXml(XmlDocument document,string xPath,string elementName,string attributeName) 
{ 
    XmlElement root = document.DocumentElement; 
    XmlNodeList nodes = root.SelectNodes(xPath); 
    for (int i = 0; i < nodes.Count; i++) 
    { 
     string attribute = nodes[i].Attributes[attributeName].Value; 
     System.Diagnostics.Debug.WriteLine(attribute); 
    } 
}