-4

enter code herein the XML document :

<foo>
  <bar><para> test </para> </bar>
  <bar>text</bar>
  <bar>stackoverflow</bar>
</foo>

I am trying to parse it and only get the Strings in bar; by using this way:

[function where node is the foo]

foreach (XmlNode subNode in node.ChildNodes)
{

 if (subNode.Name == "bar")
 {
    if (!String.IsNullOrWhiteSpace(subNode.InnerText))
     Debug.WriteLine(subNode.Name + " - " subNode.InnerText);
 }

}

However it gives me test

Thanks

5
  • I have some code that actually does do this, if you are interested I can post it. I created a web application that will read an input file from NIST and update the corresponding local database with their latest vulnerability information. The source file from them is XML so I have to parse out their data and populate my database fields with their information. Commented May 15, 2017 at 14:10
  • without XPath (so XmlDocument or XmlReader?! Why would you want to discount ways to parse the XML correctly? Commented May 15, 2017 at 14:10
  • 1
    Possible duplicate of How to read a particular node from xml in C#? Commented May 15, 2017 at 14:11
  • @bbcompent1 yes it would be nice ! Commented May 15, 2017 at 14:13
  • @XavierC. I updated my answer. Commented May 15, 2017 at 15:42

2 Answers 2

1

This is what you are looking for (EDITTED based on you updated question)

XDocument doc = XDocument.Load(path to your xml file);
XNamespace ns = "http://docbook.org/ns/docbook";

var result = doc.Root.Descendants(ns + "para")
                     .Where(x => x.FirstNode.NodeType == System.Xml.XmlNodeType.Text)
                     .Select(x => x.Value)
                     .ToList();

In your updated xml I see you are using a namespace so the name of your node is not para but it's theNameSpace + "para". The namespace is defined in the first line of your xml file. Also you can see this sample too.

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

3 Comments

Thanks for your help dude, I don't know why it doesn't work I edited my first post with the true structure but I thought my first should have work sorry.
Your xml file is not a valid one.
Your xml file is not valid yet. if you update it I can help you. otherwise you can get the whole idea from the answer.
0

Do you want "test"? Try following :

            string input =
                "<foo>" +
                  "<bar><para> test </para> </bar>" +
                  "<bar>text</bar>" +
                  "<bar>stackoverflow</bar>" +
                "</foo>";


            XDocument doc = XDocument.Parse(input);

            List<string> bars = doc.Descendants("bar").Where(x => x.NextNode != null).Select(x => (string)((XElement)x.NextNode)).ToList();

1 Comment

I don't want test because it's not in bar but in para, I edited my first post. Sorry if it was confuse

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.