2012-08-14 83 views
1

以下是( 「2015」)存儲在某個文本框的XPath C#屬性值

讀入的XmlDocument

<Test xmlns="http://api.test.com/v2" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <Result id="2015" description="Invalid Token" /> 
</Test > 

我需要的是 'id' 屬性值的XML文件,這是多麼的XmlDocument被加載

XmlDocument updateUser = new XmlDocument(); 
updateUser.Load(response.GetResponseStream()); 

很好地工作到這裏。

然後,創建命名空間和搜索節點

XmlNamespaceManager nsmgr = new XmlNamespaceManager(updateUser.NameTable); 
nsmgr.AddNamespace("restup", "http://api.test.com/v2"); 

XmlNodeList locationElements1 = updateUser.SelectNodes("//restup:Test", nsmgr); 
foreach (XmlNode Test in locationElements1) 
{ 
//What DO I do here to get the value of 'id' attribute from the 'Result' node and save it in txtTest Textbox. 

} 
+0

哦,你發佈的XML有用的另一種方法。我會改變我的答案... – 2012-08-14 15:50:22

回答

0
string idString = Test.FirstChild.Attributes["id"].ToString(); 
+1

謝謝,作品 – 2012-08-14 15:57:11

3
var id = Test.FirstChild.Attributes["id"].Value; 
+0

juan,我已經用xml更新了這個問題 – 2012-08-14 15:51:27

+0

謝謝,作品很有魅力 – 2012-08-14 15:56:37

0

您好,這是可以

XmlTextReader reader = new XmlTextReader(fileLocation); //fileLocation is the Path of the XML file 
while (reader.Read()) 
{ 

    if (reader.NodeType == XmlNodeType.Element) //if the node is an element (not a comment, CDATA, or text) 
    if (reader.Name == "Result") 
     textBox1.Text = reader.GetAttribute("id"); 

} 
reader.Close(); 
+0

wlc。當你的聲望達到15分時,別忘了投票:P – 2012-08-23 16:49:35