Click here to Skip to main content
15,891,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a nested XML document a sample of which is as follows:

XML
<?xml version="1.0" encoding="utf-8"?>
<root>
  <contact>
    <adr3 />
    <city>Alicante</city>
    <time_in_spain>
      <departed_UK>1999-09-12</departed_UK>
      <residency>2010-06-21</residency>
    </time_in_spain>
    <oap>true</oap>
    <case>
      <docs_posted>2011-06-21</docs_posted>
      <completion>2011-06-21</completion>
      <payment_recieved_uk>
        <date_received>2011-06-21</date_received>
        <transfer_date>2011-06-21</transfer_date>
      </payment_recieved_uk>
    </case>
  </contact>
</root>


I want to access the childnodes such as <date_received></date_received> by means of the indexer. I am accessing Contact.ChildNodes such as <adr3> by means of contactnode["adr3"].InnerText, but it will not work when I try either of: contactnode["date_received"].InnerText or contactnode["//date_received"].InnerText or contactnode["case/payment_receieved_uk/date_received"].InnerText. How, then, can I access these child nodes. Restructuring the database IS and option but the structure helps with management in other ways; I would prefer just to get the qualified name correct or access by some other means.
Posted
Updated 5-Jul-10 0:10am
v3

1 solution

I hope this class will help you, try XmlStuff.GetXmlStuffFromPath( string path ) for you Xml file.
Well there is the technique to get all Nodes from the Xml.

MSIL
class XmlStuff
    {
        public string adr3 { get; set; }
        public string city { get; set; }
        public string departed_UK { get; set; }
        public string residency { get; set; }
        public bool oap { get; set; }
        public string docs_posted { get; set; }
        public string completion { get; set; }
        public string date_received { get; set; }
        public string transfer_date { get; set; }
        public static XmlStuff GetXmlStuffFromPath(string path)
        {
            XmlStuff xmlStuff = new XmlStuff();
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(path);
            foreach (XmlNode xmlNode in xmlDocument.ChildNodes)
            {
                switch (xmlNode.Name)
                {
                    case "root":
                        foreach (XmlNode rootChildNode in xmlNode.ChildNodes)
                        {
                            switch (rootChildNode.Name)
                            {
                                case "contact":
                                    foreach (XmlNode contactChildNode in rootChildNode.ChildNodes)
                                    {
                                        switch (contactChildNode.Name)
                                        {
                                            case "adr3":
                                                xmlStuff.adr3 = contactChildNode.InnerText;
                                                break;
                                            case "city":
                                                xmlStuff.city = contactChildNode.InnerText;
                                                break;
                                            case "time_in_spain":
                                                foreach (XmlNode timeInSpainChildNode in contactChildNode.ChildNodes)
                                                {
                                                    switch (timeInSpainChildNode.Name)
                                                    {
                                                        case "departed_UK":
                                                            xmlStuff.departed_UK = timeInSpainChildNode.InnerText;
                                                            break;
                                                        case "residency":
                                                            xmlStuff.residency = timeInSpainChildNode.InnerText;
                                                            break;
                                                        default:
                                                            break;
                                                    }
                                                }
                                                break;
                                            case "oap":
                                                xmlStuff.oap = Convert.ToBoolean(contactChildNode.InnerText);
                                                break;
                                            case "case":
                                                foreach (XmlNode caseChildNode in contactChildNode.ChildNodes)
                                                {
                                                    switch (caseChildNode.Name)
                                                    {
                                                        case "docs_posted":
                                                            xmlStuff.docs_posted = caseChildNode.InnerText;
                                                            break;
                                                        case "completion":
                                                            xmlStuff.completion = caseChildNode.InnerText;
                                                            break;
                                                        case "payment_recieved_uk":
                                                            foreach (XmlNode paymentRecievedUkChildNode in caseChildNode.ChildNodes)
                                                            {
                                                                switch (paymentRecievedUkChildNode.Name)
                                                                {
                                                                    case "date_received":
                                                                        xmlStuff.date_received = paymentRecievedUkChildNode.InnerText;
                                                                        break;
                                                                    case "transfer_date":
                                                                        xmlStuff.transfer_date = paymentRecievedUkChildNode.InnerText;
                                                                        break;
                                                                    default:
                                                                        break;
                                                                }

                                                            }
                                                            break;
                                                        default:
                                                            break;

                                                    }
                                                }
                                                break;
                                            default:
                                                break;
                                        }

                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
            return xmlStuff;
        }

    }
 
Share this answer
 
Comments
watson128 24-Jun-10 4:47am    
Thanks for your input but it is a bit of a sledge-hammer to crack a walnut if you don't mind me saying (which you possibly do). All I really need is an explanation as to how I can supply the correct "qualified name" to the node indexer so that the innertext is available. I can see how this could be very useful if you want to access many nodes very quickly but it is very labour intensive if you only wan to access one or two of the nodes in the entire file.

I am not being snooty but I would prefer an more generic and elegant approach.

Thanks again for your input I am sure I can use it in other situations.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900