Link to home
Start Free TrialLog in
Avatar of pzozulka
pzozulka

asked on

Convert Xml to Dictionary

I have a XML document that has many nodes, with children nodes and grandchildren nodes. I need to output all nodes into a NameValueCollection or Dictionary using C#.

The Xml looks like this:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<ePolicy>
   <company_code>555</company_code>
   <details>
      <name>John</name>
   </details>
   <owner_info>
      <owners_name>
         <first>Edward</first>
         <last>Smith</last>
      </owners_name>
   </owner_info>
</ePolicy>

The following seems to work for nodes that are direct children of the root node (ePolicy), but for grandchild nodes, it just combines them all into a single value.

Here's the output I'm getting:

company_code, 555
details, John
owner_info, EdwardSmith

The above XML is a sample from URLstring variable below:

qsCollection = new System.Collections.Specialized.NameValueCollection();
            XmlTextReader reader = new XmlTextReader(URLstring);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(reader);

            XmlNodeList nodeList = xmlDoc.SelectNodes("ePolicy");

            foreach (XmlElement node in xmlDoc.DocumentElement)
            {
                qsCollection.Add(node.Name, node.InnerText);
            }

Open in new window

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

In the XML you showed in your question is that the extent of the XML document or are there many company_code and this is just one of them. Will the XML document always represent one entity?
Avatar of pzozulka
pzozulka

ASKER

It will always represent one entity.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Primarily because it is a lot of work because the XML file has over 50 elements, and to populate them all manually would be a lot of work.

Plus, there exists an entire mechanism in place that handles a lot of stuff, only if I pass it a NameValueCollection.
You don't want the child nodes to be combined into a single value. How do you want them to be handled?
I would like each and every mode, whether it's a parent, chikd, or grandchild node to be treated all the same and just be loaded into a collection.
So for this XML below, one possible collection can be:

ePolicy, <company_code>...</owner_info>
company_code, 555
details, <name>John</name>
name, John
owner_info, <first>Edw...</owner_info>
owners_name, <first>Edwar...</owners_name>
first, Edward
last, Smith

<ePolicy>
   <company_code>555</company_code>
   <details>
      <name>John</name>
   </details>
   <owner_info>
      <owners_name>
         <first>Edward</first>
         <last>Smith</last>
      </owners_name>
   </owner_info>
</ePolicy>
Another possible solution that I thought of could be to do this recursively. In other words, call a method called printNode, inside check if node has children, if so, call printNode method on child nodes all the way till you reach the final child nodes. Then recurse your way out.

I just figured there would be some existing way to print out all nodes.

My goal is to later on be able to use my collection to retrieve any mode from the original XML doc, even child nodes.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If using the easiest way, what if my entity has to XML elements that are the same element name. For example, <street> below. How do you retrieve it from the collection? Perhaps this question is the same for all the solutions listed above, not just the easiest.

<ePolicy>
        <residence_address>
            <street>123 Main St</street>
            <city>New York</city>
            <state>NY</state>
            <zip>12345</zip>
      </residence_address>
      <alternate_mailing_address>
            <street>21 Jump St</street>
            <city>New York</city>
            <state>NY</state>
            <zip>55555</zip>
      </alternate_mailing_address>
</ePolicy>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial