2012-08-14 84 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