1

The following is the XML file read into XmlDocument

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

What I need is the 'id' attribute value ("2015") stored in some TextBox

This is how XmlDocument is loaded

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

Works well till here.

Then, create namespace and search for node

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.

}
1
  • oh you posted xml. i'll change my answer... Commented Aug 14, 2012 at 15:50

3 Answers 3

3
var id = Test.FirstChild.Attributes["id"].Value;
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

hello this is another method that can be useful

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();

1 Comment

wlc. don't forget to vote up when your reputation reaches 15 points :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.