Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Reading  multiple xml tags in c#

Posted on 2009-04-24
7
Medium Priority
?
731 Views
Last Modified: 2013-12-17
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
Comment
Question by:WannabeNerd
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 9

Expert Comment

by:tetorvik
ID: 24223028
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
 
LVL 6

Expert Comment

by:HarryNS
ID: 24223148
Read xml Nodes from Delivery....
foreach (XmlNode xmlNde in xmlDoc.GetElementsByTagName("Delivery"))
            {
// Your code with xmlNde
}
0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24223222
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:WannabeNerd
ID: 24227598
HarryNS: i think you code might be what i am looking for.
Just have to wait  until i finishid other bits of code.
0
 
LVL 9

Expert Comment

by:tetorvik
ID: 24227892
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
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 24232543
           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
 

Accepted Solution

by:
WannabeNerd earned 0 total points
ID: 24335131
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question