简体   繁体   中英

Get child attributes of xml node c#

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

            }

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM