0

I am trying to get attributes from a specific location in an xml document the xml looks contains multiple similar tagnames like this:

 <Message dataItemId="Axis_01" timestamp="2018-06-25T20:20:40.4374489Z" 
 name="[#] Numero inversioni" sequence="85988" 
 nativeCode="208573">208573</Message>
 <Message dataItemId="Axis_02_InvDDone" timestamp="2018-06- 
  25T20:20:40.4374489Z" name="Error" sequence="85998" 
  nativeCode="208573">208573</Message>

how do I retrieve only the value of the Message with the name of Error? Below is my attempted code where textbox1 would = Message and textbox2 would = Error:

 XmlDocument xmldoc = new XmlDocument();
        XmlNodeList xmlnode;

        try
        {
            xmldoc.Load("http://127.0.0.1:5000/cur");
            XmlNode node2 = xmldoc.SelectSingleNode("'"+ textBox1.Text + 
            "'[name='" + textBox2.Text + "']");
               listBox1.Items.Add(node2.ChildNodes.Item(0).InnerText.Trim());

            }

2 Answers 2

2

Your XPath is a little incorrect. For the purposes of testing, I hardcoded the values:

XmlNode messageNode = xmlDoc.SelectSingleNode("//Message[@name='Error']");

I think you will be able to easily substitute the hard-coded values for the inputs from the text boxes.

What this will do is search the whole Xml document for a Message node that has an attribute called name with the value of Error. SelectSingleNode will return the first occurrence if there are multiple matches. There is a SelectNodes function that will return multiple values, if you need it.

The important bits are:

  1. \\Message - which instructs the XmlDocument to find the Message node
  2. @name- instructs the XmlDocument to look for an attribute

When I ran this it found:

<Message dataItemId="Axis_02_InvDDone" timestamp="2018-06-25T20:20:40.4374489Z" name="Error" sequence="85998" nativeCode="208573">208573</Message>

I am not clear on which attribute you want to retrieve. This will retrieve the value of the dataItemId attribute

Debug.Print(messageNode.Attributes["dataItemId"].InnerText);

Axis_02_InvDDone

To get the Text value of that node, ie 208573, use:

Debug.Print(messageNode.InnerText);
Sign up to request clarification or add additional context in comments.

2 Comments

This works well! how do I am trying to retrieve the Message child node as follows but it keeps giving me an error: listBox1.Items.Add(messagenode.ChildNodes.Item(0).InnerText.Trim()); that would return 208573 in a node list
@Marcus To get the Text value of that node, use messageNode.InnerText
0

Below is the code from following link: https://msdn.microsoft.com/en-us/library/system.xml.xmlnode.childnodes(v=vs.110).aspx

public class Sample {

  public static void Main() {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "<price>19.95</price>" +
                "</book>");

    XmlNode root = doc.FirstChild;

    //Display the contents of the child nodes.
    if (root.HasChildNodes)
    {
      for (int i=0; i<root.ChildNodes.Count; i++)
      {
        Console.WriteLine(root.ChildNodes[i].InnerText);
      }
    }
  }
}

You can use condition to get specific Child Node.

Comments

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.