简体   繁体   中英

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...

Updated code based on leo suggestion.

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

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

not working.

Updated code as per salem22 suggetions. No business rule or logic return.After reading just going over foreach loop to run it, not working.

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

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

Adding the actual file that i am seeing after reading in xdoc

<customers xmlns="http://www.demandware.com/xml/impex/customer/2007-05-31">
 <customer-list>
 <customer>
  <FirstName>B</FirstName> 
  <LastName>C</LastName> 
  <Email></Email> 
  <OptInEmail>1</OptInEmail> 
  <Phone>9056953000</Phone> 
  <Address1>On</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>
  </customers>

Try this:

  XDocument xd=Xdocument.Load(path);
  XNamespace ns = "http://www.demandware.com/xml/impex/customer/2007-05-31";

  var customer=from node in xd.Descendants(ns+"customer")
               select new
               {
                 FirstName=node.Element(ns+"FirstName").Value,
                 ...
               };

You have Elements not Attributes . Use XElement.Element(string) method

FirstName = (string)node.Element("FirstName"),
LastName = (string)node.Element("LastName"),
...

Also don't use the Value property if you are making explicit cast. it's pointless. Value is already a string, and it will throw an exception if the element wasn't found.

You need to change the call to Attribute with Element because your customer don't have any attributes and you are interested in getting the inner elements' text. Replace all method calls similar to the one below...

FirstName = (string)node.Attribute("FirstName").Value,

with this...

FirstName = node.Element("FirstName").Value,

You get nothing because there aren't any attributes to read.The element "customer" is a complexType element that has children elements (firstname,lastname, etc.) . In order to read their values you have to change the method from node.Attribute to node.Element . With the XML you posted i runned the following code and got the desired results.

var xdoc = XDocument.Load(path);
var customer = from node in xdoc.Descendants("customer")

select new
{
    FirstName = node.Element("FirstName").Value.ToString(),
    LastName = node.Element("LastName").Value.ToString(),
    Email = node.Element("Email").Value.ToString(),

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM