1

I have been trying to read an xml file. I have to extract value of nodes "Date" and "Name", but the problem is, they might appear at any level in XML hierarchy.

So when I try with this code,

        XmlDocument doc = new XmlDocument();
        doc.Load("test1.xml");
        XmlElement root = doc.DocumentElement;
        XmlNodeList nodes = root.SelectNodes("//*");
        string date;
        string name;

        foreach (XmlNode node in nodes)
        {
                    date = node["date"].InnerText;
                    name = node["name"].InnerText;
        }

and the XML file is ::

<?xml version="1.0" encoding="utf-8"?>
<root>
<child>
  <name>Aravind</name>
  <date>12/03/2000</date>
</child>
</root>

the above code errors out, as <name> and <date> are not immediate child Elements of root.
is it possible to assume that parent/root nodes are unknown and just with the name of the nodes, copy the values ??

3 Answers 3

2

Depending on the exception you are getting, this may or may not be the exact solution. However, I would definitely check that date and name exist before doing a .InnerText on them.

    foreach (XmlNode node in nodes)
    {
                dateNode = node["date"];
                if(dateNode != null)
                    date = dateNode.InnerText;
                // etc.
    }
Sign up to request clarification or add additional context in comments.

1 Comment

with necessary basic edits and work-around, answer can be accepted. :) @Mark. thanks for the valuable post.
2

I would read up on XPATH and XPATH for C# to do this more efficiently

http://support.microsoft.com/kb/308333

http://www.w3schools.com/XPath/xpath_syntax.asp

Here's a little method that should allow you to get the innerText easily.

function string GetElementText(string xml, string node)
{
    XPathDocument doc = new XPathDocument(xml);
    XPathNavigator nav = doc.CreateNavigator();

    XPathExpression expr = nav.Compile("//" + node);
    XPathNodeIterator iterator = nav.Select(expr);

    while (iterator.MoveNext())
    {
        // return 1st but there could be more
        return iterator.Current.Value;            
    }
}

1 Comment

Thanks for the valuable time and answer. :)
1

Try to use LINQ:

        string xml = @"<?xml version='1.0' encoding='utf-8'?>
                       <root>
                       <date>12/03/2001</date>
                       <child>
                         <name>Aravind</name>
                         <date>12/03/2000</date>
                       </child>
                       <name>AS-CII</name>
                       </root>";

        XDocument doc = XDocument.Parse(xml);

        foreach (var date in doc.Descendants("date"))
        {
            Console.WriteLine(date.Value);
        }

        foreach (var date in doc.Descendants("name"))
        {
            Console.WriteLine(date.Value);
        }

        Console.ReadLine();

The Descendants method allows you to get all the elements that have a specified name.

2 Comments

can you please tell me which namespace should be used?
Add System.Xml.Linq as reference and also as using.

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.