<?xml version="1.0" encoding="UTF-8"?>
<Orders xmlns="www.test-inc.com">
<Order>
<Header>
<Address>
<AddressTypeCode>ST</AddressTypeCode>
<LocationCodeQualifier>51</LocationCodeQualifier>
<AddressName>Pepsi Distribution Center</AddressName>
<AddressAlternateName>ABC 123</AddressAlternateName>
<Address1>Safeway 7th Street</Address1>
<Address2>Suite 1</Address2>
<City>Detroit</City>
<State>MI</State>
<PostalCode>12345</PostalCode>
<Country>USA</Country>
</Address>
<Address>
<AddressTypeCode>BT</AddressTypeCode>
<LocationCodeQualifier>51</LocationCodeQualifier>
<AddressLocationNumber>15513432</AddressLocationNumber>
<AddressName>Main Headquarters</AddressName>
<AddressAlternateName>Attn: John</AddressAlternateName>
<Address1>147 Dupont Ave</Address1>
<Address2>Dock 123</Address2>
<City>Orlando</City>
<State>FL</State>
<PostalCode>12345</PostalCode>
<Country>USA</Country>
<Contact>
<ContactTypeCode>BD</ContactTypeCode>
<ContactName>Jane Doe</ContactName>
<PrimaryPhone>111-222-3333</PrimaryPhone>
<PrimaryFax>111-222-3333</PrimaryFax>
<PrimaryEmail>buyer@TEST.com</PrimaryEmail>
</Contact>
</Address>
</Header>
// Ship to Address
var addresses = xdoc.XPathSelectElements("//testinc:Address", nsmgr);//Get all the address childs : 2 in this case
List<string> listofAddresses = new List<string>();//List to store the addresses
foreach (XElement address in addresses)//For each line item
{
//something here?
}
// Load Document
XDocument xdoc = XDocument.Load("Path and file name of the XML doc");
// Get a ref to the XML Namespace
XNamespace ns = xdoc.Root.Name.Namespace;
// Get the Ship to Address
var addresses = xdoc.Root.Descendants(ns + "Address").ToList();
// If you want the Ship To address it will be element 0 and the Bill To address is element 1
// XDocuments are process in document order
int idx = 0; // 0 = ST and 1 = BT
// Get the proper address
XElement addInfo = addresses[idx];
Console.WriteLine("{0} {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8} ",
(idx == 0) ? "The ship to address is:" : " The bill to address is:",
addInfo.Element(ns + "AddressName").Value,
addInfo.Element(ns + "AddressAlternateName").Value,
addInfo.Element(ns + "Address1").Value,
addInfo.Element(ns + "Address2").Value,
addInfo.Element(ns + "City").Value,
addInfo.Element(ns + "State").Value,
addInfo.Element(ns + "PostalCode").Value,
addInfo.Element(ns + "Country").Value
);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
namespace Q28909699_TEKNOVATION_CleanCode
{
class Program
{
static void Main(string[] args)
{
string filepath = @"E:\tsip\codeblocks_examples\expertsexchange\TotalEESolution\Q28909699_TEKNOVATION_CleanCode\TestAddress.xml";
XDocument xdoc = XDocument.Load(filepath);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
XNamespace ns = "www.test-inc.com";
var stadd = (from saddress in xdoc.Descendants(ns + "Address")
where saddress.Element(ns + "AddressTypeCode").Value == "ST"
select new
{
Address = string.Join(",", saddress.Elements().Where(y => y.Name != ns + "AddressTypeCode" && y.Name != ns + "Contact").Select(z => z.Value).ToList())
});
var btadd = (from saddress in xdoc.Descendants(ns + "Address")
where saddress.Element(ns + "AddressTypeCode").Value == "BT"
select new
{
Address = string.Join(",", saddress.Elements().Where(y => y.Name != ns + "AddressTypeCode" && y.Name != ns + "Contact").Select(z => z.Value).ToList())
});
//Assumption is that there shall always be one ship to address and bill to address at any case, else First() might fail.
Console.WriteLine("The ship to address is: {0}", stadd.AsEnumerable().First().Address);
Console.WriteLine("The bill to address is: {0}", btadd.AsEnumerable().First().Address);
Console.ReadLine();
}
}
}
Output:<?xml version="1.0" encoding="UTF-8"?>
<Orders xmlns="www.test-inc.com">
<Order>
<Header>
<Address>
<AddressTypeCode>ST</AddressTypeCode>
<LocationCodeQualifier>51</LocationCodeQualifier>
<AddressName>Pepsi Distribution Center</AddressName>
<AddressAlternateName>ABC 123</AddressAlternateName>
<Address1>Safeway 7th Street</Address1>
<Address2>Suite 1</Address2>
<City>Detroit</City>
<State>MI</State>
<PostalCode>12345</PostalCode>
<Country>USA</Country>
</Address>
<Address>
<AddressTypeCode>BT</AddressTypeCode>
<LocationCodeQualifier>51</LocationCodeQualifier>
<AddressLocationNumber>15513432</AddressLocationNumber>
<AddressName>Main Headquarters</AddressName>
<AddressAlternateName>Attn: John</AddressAlternateName>
<Address1>147 Dupont Ave</Address1>
<Address2>Dock 123</Address2>
<City>Orlando</City>
<State>FL</State>
<PostalCode>12345</PostalCode>
<Country>USA</Country>
<Contact>
<ContactTypeCode>BD</ContactTypeCode>
<ContactName>Jane Doe</ContactName>
<PrimaryPhone>111-222-3333</PrimaryPhone>
<PrimaryFax>111-222-3333</PrimaryFax>
<PrimaryEmail>buyer@TEST.com</PrimaryEmail>
</Contact>
</Address>
</Header>
</Order>
</Orders>
<Contact>
<ContactTypeCode>BD</ContactTypeCode>
<ContactName>Jane Doe</ContactName>
<PrimaryPhone>111-222-3333</PrimaryPhone>
<PrimaryFax>111-222-3333</PrimaryFax>
<PrimaryEmail>buyer@TEST.com</PrimaryEmail>
</Contact>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
namespace Q28909699_TEKNOVATION_CleanCode
{
class Program
{
static void Main(string[] args)
{
string filepath = @"E:\tsip\codeblocks_examples\expertsexchange\TotalEESolution\Q28909699_TEKNOVATION_CleanCode\TestAddress.xml";
XDocument xdoc = XDocument.Load(filepath);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
XNamespace ns = "www.test-inc.com";
nsmgr.AddNamespace("testinc", "www.test-inc.com");
var stadd = (from saddress in xdoc.Descendants(ns + "Address")
where saddress.Element(ns + "AddressTypeCode").Value == "ST"
select new
{
Address = string.Join(",", saddress.Elements().Where(y => y.Name != ns + "AddressTypeCode" && y.Name != ns + "Contact").Select(z => z.Value).ToList())
});
var btadd = (from saddress in xdoc.Descendants(ns + "Address")
where saddress.Element(ns + "AddressTypeCode").Value == "BT"
select new
{
Address = string.Join(",", saddress.Elements().Where(y => y.Name != ns + "AddressTypeCode" && y.Name != ns + "Contact").Select(z => z.Value).ToList())
});
//Assumption is that there shall always be one ship to address and bill to address at any case, else First() might fail
Console.WriteLine("The ship to address is: {0}", stadd.AsEnumerable().First().Address);
Console.WriteLine("The bill to address is: {0}", btadd.AsEnumerable().First().Address);
var staddelements = (from saddress in xdoc.Descendants(ns + "Address")
where saddress.Element(ns + "AddressTypeCode").Value == "ST"
select saddress.Descendants()).FirstOrDefault();
Console.WriteLine();
Console.WriteLine("*****************************Shipping Address Start**********************************");
foreach (var line in staddelements){
Console.WriteLine(line.Name.LocalName + " : " + line.Value);
}
Console.WriteLine("*****************************Shipping Address End**********************************");
Console.WriteLine();
Console.WriteLine("*****************************Billing Address Start**********************************");
var btaddelements = (from saddress in xdoc.Descendants(ns + "Address")
where saddress.Element(ns + "AddressTypeCode").Value == "BT"
select saddress.Descendants()).FirstOrDefault();
foreach (var line in btaddelements)
{
Console.WriteLine(line.Name.LocalName + " : " + line.Value);
}
Console.WriteLine("*****************************Billing Address End**********************************");
Console.ReadLine();
}
}
}
XML File<?xml version="1.0" encoding="UTF-8"?>
<Orders xmlns="www.test-inc.com">
<Order>
<Header>
<Address>
<AddressTypeCode>ST</AddressTypeCode>
<LocationCodeQualifier>51</LocationCodeQualifier>
<AddressName>Pepsi Distribution Center</AddressName>
<AddressAlternateName>ABC 123</AddressAlternateName>
<Address1>Safeway 7th Street</Address1>
<Address2>Suite 1</Address2>
<City>Detroit</City>
<State>MI</State>
<PostalCode>12345</PostalCode>
<Country>USA</Country>
</Address>
<Address>
<AddressTypeCode>BT</AddressTypeCode>
<LocationCodeQualifier>51</LocationCodeQualifier>
<AddressLocationNumber>15513432</AddressLocationNumber>
<AddressName>Main Headquarters</AddressName>
<AddressAlternateName>Attn: John</AddressAlternateName>
<Address1>147 Dupont Ave</Address1>
<Address2>Dock 123</Address2>
<City>Orlando</City>
<State>FL</State>
<PostalCode>12345</PostalCode>
<Country>USA</Country>
<Contact>
<ContactTypeCode>BD</ContactTypeCode>
<ContactName>Jane Doe</ContactName>
<PrimaryPhone>111-222-3333</PrimaryPhone>
<PrimaryFax>111-222-3333</PrimaryFax>
<PrimaryEmail>buyer@TEST.com</PrimaryEmail>
</Contact>
</Address>
</Header>
</Order>
</Orders>
Output: var btaddreselements = (from saddress in xdoc.Descendants(ns + "Address")
where saddress.Element(ns + "AddressTypeCode").Value == "BT"
select saddress).FirstOrDefault();
//Address Name
var addressname = btaddreselements.Descendants(ns + "AddressName").FirstOrDefault();
Console.WriteLine(addressname.Name.LocalName + " : " + addressname.Value);
//Contact Name
var contactName = btaddreselements.Descendants(ns + "ContactName").FirstOrDefault();
Console.WriteLine(contactName.Name.LocalName + " : " + contactName.Value);
//PrimaryPhone
var primaryphone = btaddreselements.Descendants(ns + "PrimaryPhone").FirstOrDefault();
Console.WriteLine(primaryphone.Name.LocalName + " : " + primaryphone.Value);
var staddelements = (from saddress in xdoc.Descendants(ns + "Address")
where saddress.Element(ns + "AddressTypeCode").Value == "ST"
select saddress.Descendants()).FirstOrDefault();
var btaddelements = (from saddress in xdoc.Descendants(ns + "Address")
where saddress.Element(ns + "AddressTypeCode").Value == "BT"
select saddress.Descendants()).FirstOrDefault();
var addressname = btaddelements.Descendants(ns + "AddressName").FirstOrDefault();
Console.WriteLine(addressname.Name.LocalName + " : " + addressname.Value);