• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 818
  • Last Modified:

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

0
WannabeNerd
Asked:
WannabeNerd
  • 2
  • 2
  • 2
  • +1
1 Solution
 
tetorvikCommented:
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
0
 
HarryNSCommented:
Read xml Nodes from Delivery....
foreach (XmlNode xmlNde in xmlDoc.GetElementsByTagName("Delivery"))
            {
// Your code with xmlNde
}
0
 
HarryNSCommented:
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

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
WannabeNerdAuthor Commented:
HarryNS: i think you code might be what i am looking for.
Just have to wait  until i finishid other bits of code.
0
 
tetorvikCommented:
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

0
 
angus_young_acdcCommented:
           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
            }
0
 
WannabeNerdAuthor Commented:
Well i have taken much reliable approace by creating  c# clases and then deserialization the xml file.
So far so good but having a  bit problem.

How to create a classs for:
<Address>
                              <AddressLine>abc</AddressLine>
                              <AddressLine>abc</AddressLine>
                              <AddressLine>abc</AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine></AddressLine>
                              <AddressLine>aaaaaa</AddressLine>
                        </Address>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now