如何到达特定节点并检索数据

时间:2015-02-25 18:49:19

标签: c# linq-to-xml

继承我的XML

 <metadata created="2015-02-24T17:50:40.188Z" xmlns="http://example.com" xmlns:ext="http://example.com">
  <customer-group-list count="10">
    <customer-group id="790174c22752" type="Corporate">
      <title>Mr</title>
      <primary-type>Corporate</primary-type>
      <customer-credit>
        <name-credit>
          <customer id="3d57f91ecf5e">
            <name>Michael Jackson</name>
            <sort-name>Jackson, Michael</sort-name>
            <alias-list>
              <alias sort-name="JACKSON MICHAEL JOE">JACKSON MICHAEL JOE</alias>
            </alias-list>
          </customer>
        </name-credit>
      </customer-credit>
    </customer-group>
    </customer-group-list>
    </metadata>

我试图获得(客户ID),所以我有这个代码

XDoc = XDocument.Parse(XDoc.ToString());

GetCustomers = from c in XDoc.Descendants(ns + "customer-group")
               select
               new Customer
               {
                   ID = c.Element(ns + "customer-credit").Elements(ns + "name-credit").Any()
                     ? c.Element(ns + "customer").Attribute(ns + "id").Value
                     : "",
                   Title = c.Element(ns + "title").Value,
               };

所以代码的ID行是我试图嵌入我需要的节点然后获取ID但我得到的对象#34;对象没有设置为实例&#34;错误并且不确定是否有办法做到这一点?

我以为我理解了这一点,但如果你发布了一个答案我会很感激如何以这种方式获得嵌套元素的任何解释。

由于

1 个答案:

答案 0 :(得分:0)

代码中的问题在ID行

? c.Element(ns + "customer").Attributes(ns + "id").Value

你不能只跳到这样的内部元素。如果您希望查询起作用,可以使用

? c.Descendants(ns + "customer").Attributes("id").Value

如果你能用提示找出解决方案。这是完整的代码

void static Main()
{
    var xml= @"<metadata created=""2015-02-24T17:50:40.188Z"" xmlns=""http://example.com"" xmlns:ext=""http://example.com"">
<customer-group-list count=""10"">
    <customer-group id=""790174c22752"" type=""Corporate"">
    <title>Mr</title>
    <primary-type>Corporate</primary-type>
    <customer-credit>
        <name-credit>
        <customer id=""3d57f91ecf5e"">
            <name>Michael Jackson</name>
            <sort-name>Jackson, Michael</sort-name>
            <alias-list>
            <alias sort-name=""JACKSON MICHAEL JOE"">JACKSON MICHAEL JOE</alias>
            </alias-list>
        </customer>
        </name-credit>
    </customer-credit>
    </customer-group>
    </customer-group-list>
    </metadata>";

    XNamespace nameSpace  = "http://example.com";
    XElement dataSet1Tree = XElement.Parse(xml);
    var dataSet1List = from cg in dataSet1Tree.Descendants(nameSpace +"customer-group")
                    let customer = cg.Descendants(nameSpace +"customer").FirstOrDefault()
                    let title  = cg.Descendants(nameSpace +"title").FirstOrDefault()

                    select new {
                                    ID =   customer == null ? "" : customer.Attributes("id").First().Value,
                                    Title = title == null? null : title.Value
                                };

    foreach(var cust in dataSet1List)
    {
        Console.WriteLine(cust.ID +" " + cust.Title);
    }

}