使用Linq返回空对象读取XMl

时间:2014-09-04 08:42:39

标签: c# xml linq

我正在尝试阅读xml

示例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>

阅读代码

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;
            }
        }

但每次我尝试访问它时,Cutomer都是空的。没有从xml读取的值?任何建议......

根据青少狮建议更新了代码。

 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;
        }
    }

无效。

根据salem22建议更新了代码。没有业务规则或逻辑返回。阅读后只需要通过foreach循环来运行它,无法正常工作。

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;
            }
        }

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>

4 个答案:

答案 0 :(得分:2)

试试这个:

  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,
                 ...
               };

答案 1 :(得分:0)

Elements没有Attributes。使用XElement.Element(string)方法

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

如果您要进行显式演员,也不要使用Value属性。它没有意义。 Value已经是一个字符串,如果找不到该元素,它将抛出异常。

答案 2 :(得分:0)

您需要使用Attribute将来电更改为Element,因为您的customer没有任何属性,并且您有兴趣获取内部元素&#39;文本。替换所有类似于以下方法的方法调用...

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

有了......

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

答案 3 :(得分:-1)

没有任何属性可以读取。元素“customer”是一个complexType元素,它包含子元素(firstname,lastname等)。为了读取它们的值,您必须将方法从node.Attribute更改为node.Element。 使用您发布的XML,我运行了以下代码并获得了预期的结果。

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);
    }