Rookie Programmer Aravind
Rookie Programmer Aravind

Reputation: 12154

Problem in reading XML node with unknown root/parent nodes

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 ??

Upvotes: 1

Views: 2805

Answers (3)

as-cii
as-cii

Reputation: 13029

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.

Upvotes: 1

hunter
hunter

Reputation: 63532

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

Upvotes: 2

Mark Avenius
Mark Avenius

Reputation: 13947

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.
    }

Upvotes: 2

Related Questions