Link to home
Start Free TrialLog in
Avatar of WannabeNerd
WannabeNerd

asked on

Reading multiple xml tags in c#

I am developing a system in which i try to read an xml.
I have to take Address data from it.

As you can see that address tag appears more then once.
How can i make it sure i take only Delivery>DeliverTo >Address  fields only.
Right now i am trying to get addres by counting child nodes but it fails if there is more then one child node in some file.

<Buyer>
                        <BuyerReferences>
                              <SuppliersCodeForBuyer>abc</SuppliersCodeForBuyer>
                              <GLN>CH</GLN>
                        </BuyerReferences>
                        <Party>abc</Party>
                        <Address>
                              <AddressLine>abc</AddressLine>
                              <AddressLine>abc</AddressLine>
                              <AddressLine>abc</AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine>aaaaaa</AddressLine>
                        </Address>
                  </Buyer>
                  <Delivery>
                        <DeliverTo>
                              <DeliverToReferences>
                                    <BuyersCodeForDelivery>Cfr</BuyersCodeForDelivery>
                                    <GLN>CH</GLN>
                              </DeliverToReferences>
                              <Party>trest</Party>
                              <Address>
                                    <AddressLine>f  WAY</AddressLine>
                                    <AddressLine>sds</AddressLine>
                                    <AddressLine>sds</AddressLine>
                                    <AddressLine></AddressLine>
                                    <AddressLine></AddressLine>
                                    <AddressLine>sdsds</AddressLine>
                              </Address>
                        </DeliverTo>                        
                  </Delivery>
public void getAddress(XmlNodeList nList)
        {
            foreach (XmlNode n1 in nList)
            {
                if (n1.ChildNodes.Count <= 1)
                {
                    foreach (XmlNode n2 in n1)
                    {
                        foreach (XmlNode n3 in n2)
                        {
                            if(n3.Name=="Party")
                            {
                                Party = n3.InnerText;
                            }
                            else if (n3.Name == "Address")
                            {
                                int i = 0;
                                foreach (XmlNode n4 in n3)
                                {
                                    if (i <= n3.ChildNodes.Count)
                                    {
                                        DeliveryAddressLine[i] = n4.InnerText;
                                        i++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

Open in new window

Avatar of tetorvik
tetorvik
Flag of Finland image

You can you XPath to select the nodes you need. Like:
XmlNodeList nodeList = yourXmlDocument.SelectNodes("/Delivery/DeliverTo/Address");

See more about Xpath syntax
http://www.w3schools.com/XPath/xpath_syntax.asp
Avatar of HarryNS
HarryNS

Read xml Nodes from Delivery....
foreach (XmlNode xmlNde in xmlDoc.GetElementsByTagName("Delivery"))
            {
// Your code with xmlNde
}
I have tested the following code. This works!!!
private void LoadXML()
        {
            string strFile = @"C:\\Test.XML";
 
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(strFile);
            string strValue = "";
 
            foreach (XmlNode xmlNde in xmlDoc.GetElementsByTagName("Delivery"))
            {
                foreach (XmlNode xmlNde1 in xmlNde.ChildNodes[0].ChildNodes)
                {
                    if (xmlNde1.Name == "Address")
                    {
                        foreach (XmlNode xmlNde2 in xmlNde1.ChildNodes)
                        {
                            if (xmlNde2 != null && xmlNde2.FirstChild != null)
                                strValue += xmlNde2.FirstChild.Value + "\r\n";
                        }
                        MessageBox.Show(strValue);
                    }
                }                    
            }
        }

Open in new window

Avatar of WannabeNerd

ASKER

HarryNS: i think you code might be what i am looking for.
Just have to wait  until i finishid other bits of code.
The code below returns exactly the same with XPath.
XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/XMLFile1.xml"));
            XmlNodeList nodes = doc.SelectNodes("//Delivery/DeliverTo/Address/AddressLine");
            foreach (XmlNode addressLineNode in nodes)
            {
                if (addressLineNode != null && addressLineNode.FirstChild != null)
                    strValue += addressLineNode.FirstChild.Value + "\r\n";
            }

Open in new window

Avatar of angus_young_acdc
           XmlDocument doc = new XmlDocument();
            doc.Load("Test.xml");
            XmlNodeList list = doc.SelectNodes("/Delivery/DeliverTo/Address");
            XmlNode node;
            for (int i = 0; i < list.Count; i++)
            {
                // Read the nodes
            }
ASKER CERTIFIED SOLUTION
Avatar of WannabeNerd
WannabeNerd

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