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 </Supplier sCodeForBu yer>
<GLN>CH</GLN>
</BuyerReferences>
<Party>abc</Party>
<Address>
<AddressLine>abc</AddressL ine>
<AddressLine>abc</AddressL ine>
<AddressLine>abc</AddressL ine>
<AddressLine></AddressLine >
<AddressLine></AddressLine >
<AddressLine>aaaaaa</Addre ssLine>
</Address>
</Buyer>
<Delivery>
<DeliverTo>
<DeliverToReferences>
<BuyersCodeForDelivery>Cfr </BuyersCo deForDeliv ery>
<GLN>CH</GLN>
</DeliverToReferences>
<Party>trest</Party>
<Address>
<AddressLine>f WAY</AddressLine>
<AddressLine>sds</AddressL ine>
<AddressLine>sds</AddressL ine>
<AddressLine></AddressLine >
<AddressLine></AddressLine >
<AddressLine>sdsds</Addres sLine>
</Address>
</DeliverTo>
</Delivery>
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
<GLN>CH</GLN>
</BuyerReferences>
<Party>abc</Party>
<Address>
<AddressLine>abc</AddressL
<AddressLine>abc</AddressL
<AddressLine>abc</AddressL
<AddressLine></AddressLine
<AddressLine></AddressLine
<AddressLine>aaaaaa</Addre
</Address>
</Buyer>
<Delivery>
<DeliverTo>
<DeliverToReferences>
<BuyersCodeForDelivery>Cfr
<GLN>CH</GLN>
</DeliverToReferences>
<Party>trest</Party>
<Address>
<AddressLine>f WAY</AddressLine>
<AddressLine>sds</AddressL
<AddressLine>sds</AddressL
<AddressLine></AddressLine
<AddressLine></AddressLine
<AddressLine>sdsds</Addres
</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++;
}
}
}
}
}
}
}
}
Read xml Nodes from Delivery....
foreach (XmlNode xmlNde in xmlDoc.GetElementsByTagNam e("Deliver y"))
{
// Your code with xmlNde
}
foreach (XmlNode xmlNde in xmlDoc.GetElementsByTagNam
{
// 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);
}
}
}
}
ASKER
HarryNS: i think you code might be what i am looking for.
Just have to wait until i finishid other bits of code.
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";
}
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
}
doc.Load("Test.xml");
XmlNodeList list = doc.SelectNodes("/Delivery
XmlNode node;
for (int i = 0; i < list.Count; i++)
{
// Read the nodes
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
XmlNodeList nodeList = yourXmlDocument.SelectNode
See more about Xpath syntax
http://www.w3schools.com/XPath/xpath_syntax.asp