Read XMl using Linq returning empty object



I am trying to read a xml


Sample XML



<customer-list>
<customer>
<FirstName>B</FirstName>
<LastName>C</LastName>
<Email></Email>
<OptInEmail>1</OptInEmail>
<Phone>9056953000</Phone>
<Address1></Address1>
<Address2>70 East Beaver Creek Rd</Address2>
<City>Richmond Hill</City>
<State>ON</State>
<ZipCode>L4B3B2</ZipCode>
<CountryCode>CA</CountryCode>
</customer>
<customer>
<FirstName>P</FirstName>
<LastName>M</LastName>
<Email></Email>
<OptInEmail>1</OptInEmail>
<Phone>7045955246</Phone>
<Address1>Residence Inn</Address1>
<Address2>55 Minthorn Blvd</Address2>
<City>Markham</City>
<State>ON</State>
<ZipCode>L3T7Y9</ZipCode>
<CountryCode>CA</CountryCode>
</customer>
</customer-list>


Reading Code



public void ReadXml(string path)
{
var xdoc = XDocument.Load(path);
var customer = from node in xdoc.Descendants("customer")
select new
{
FirstName = (string)node.Attribute("FirstName").Value,
LastName = (string)node.Attribute("LastName").Value,
Email = (string)node.Attribute("Email").Value,
OptInEmail = (string)node.Attribute("OptInEmail").Value,
Phone = (string)node.Attribute("Phone").Value,
Address1 = (string)node.Attribute("Address1").Value,
Address2 = (string)node.Attribute("Address2").Value,
City = (string)node.Attribute("City").Value,
State = (string)node.Attribute("State").Value,
ZipCode = (string)node.Attribute("ZipCode").Value,
CountryCode = (string)node.Attribute("CountryCode").Value
};

foreach (var item in customer)
{
var test = item.FirstName;
}
}


but every time i try to access it , Cutomer is empty. No values read from xml?? Any suggestion


No comments:

Post a Comment