0

I am parsing a XML document using Xpath. The steps node have Attributes in the form of step. I am trying to get the value of each step. For example 0.03, 0.025,0.05.... But with my Code I only get the 1st value i.e 0.03 in the list of strings. Here is the Code that I tried:

XML file

private void btn_steps_Click(object sender, EventArgs e)
{
    List<string> step = new List<string>();

    XmlDocument doctst = new XmlDocument();

    doctst.Load(@"C:\ehData\workData\mywork.xml");

    XmlNodeList nodelistst = doctst.Selectnodes("//steps");

    foreach (XmlNode node in nodelistst)
    {
        step.Add(node["step"].InnerText);
    }

    listBox2.DataSource = step;
}

Moreover, I am attaching the XML file image that I am trying to parse. What I am doing wrong in this Code?

Here are the few lines of the XML file

<devices orderNo="67354698">
    <device serno="P1002001190">
      <steps>
        <step descriptor="160000556" element="1" usage="B">0.03</step>
        <step descriptor="160000556" element="2" usage="B">0.025</step>
        <step descriptor="160000556" element="3" usage="B">0.05</step>
4
  • Please post the Xml data as text here - don't expect people here to re-type your data just to reproduce your problem and help you. Commented Apr 4, 2019 at 11:32
  • 2
    Much easier to do use Xml Linq which is a new Net a Library method than standard xml library. Commented Apr 4, 2019 at 11:34
  • I have added the XML file as an Image. Please look at it. I cannot post the text bcauseit is very Long. Commented Apr 4, 2019 at 11:44
  • @Sohaib, I added my answer with output below try it and let me know :) Commented Apr 4, 2019 at 12:14

2 Answers 2

1

1) By using XmlDocument.GetElementsByTagName() with xml tag name

...

XmlNodeList nodelistst1 = doctst.GetElementsByTagName("step");

foreach (XmlNode node in nodelistst1)
{
    step.Add(node.InnerText);
}

2) By using XmlDocument.SelectNodes() with XPath

...

XmlNodeList nodelistst = doctst.SelectNodes(".//devices/device/steps/step/text()");

foreach (XmlNode node in nodelistst)
{
    step.Add(node.Value);
}

So finally your result from both of the above option would be

foreach (var item in step)
{
    Console.WriteLine(item);
}

Output: (For provided text XML from OP)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure whether you want attribute value or you want inner text. Hence I am putting everything here.

Retrieving attributes:

For getting descriptor attribute from first step element - you can use below XPath:

/devices/device/steps/step[1]/@descriptor

This XPath will return - descriptor="160000556"

If you want only value then you can use:

string(/devices/device/steps/step[1]/@descriptor)

This would return only value i.e. 160000556

Same strategy you can apply for element or usage attributes.

Retrieving Inner Text:

And for getting inner text of second step element, you can use below XPath:

/devices/device/steps/step[2]/text()

Now these XPaths you can run through loop and change index inside square brackets to get all attributes and elements.

Hope this helps.

1 Comment

HI. Thanks for Posting the Code. Actually I want the inner-text i.e 0.03,0.025,0.5 in a list of string.

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.