Inner XML tag Parsing Using C#

user3524976 picture user3524976 · Apr 11, 2014 · Viewed 8.8k times · Source
<career code="17-1011.00">
   <code>17-1011.00</code>
   <title>Architects</title>
   <tags bright_outlook="false" green="true" apprenticeship="false" />
   <also_called>
      <title>Architect</title>
      <title>Project Architect</title>
      <title>Project Manager</title>
      <title>Architectural Project Manager</title>
   </also_called>
   <what_they_do>Plan and design structures, such as private residences, office buildings, theaters, factories, and other structural property.</what_they_do>
   <on_the_job>
      <task>Consult with clients to determine functional or spatial requirements of structures.</task>
      <task>Prepare scale drawings.</task>
      <task>Plan layout of project.</task>
   </on_the_job>
</career>

I have taken this XML returned from ONet and would like to parse the information to use. Here is the code that I have written to try and and parse the inner text of the tags under , with the 'input' being the Onet XML.

 XmlDocument inputXML = new XmlDocument();
        inputXML.LoadXml(input);
        XmlElement root = inputXML.DocumentElement;
        XmlNodeList titleList = root.GetElementsByTagName("also_called");
        for (int i = 0; i < titleList.Count; i++)
        {
            Console.WriteLine(titleList[i].InnerText);
        } 

I am expecting a NodeList of size four. However when I print the result out, the result is a size of 1: "ArchitectProject ArchitectProject ManagerArchitectural Project Manager"

Have I constructed my XMLNodeList titleList wrong? How can I further traverse and process the XML tree to get the inner values of the 'title' tags under 'also_called'?

Answer

Jean Hominal picture Jean Hominal · Apr 11, 2014

You get the elements named also_called. There is only one such element in your list. What you probably want is to get the children of the also_called node.

For example:

XmlNodeList also_calledList = root.GetElementsByTagName("also_called");
XmlNode also_calledElement = also_calledList[0];
XmlNodeList titleList = also_calledElement.ChildNodes;

foreach (XmlNode titleNode in titleList)
{
    Console.WriteLine(titleNode.InnerText);
}

Also, consider using XDocument and LINQ to XML instead of XmlDocument - it is a lot simpler to use:

XDocument root = XDocument.Parse(input);

foreach (XElement titleNode in root.Descendants("also_called").First().Elements())
{
    Console.WriteLine(titleNode.Value);
}