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

How to get to specific node and retrieve data

这是我的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 但我收到 "Object not set to an instance" 错误并且不确定是否有办法做到这一点的地方?

我以为我理解了这一点,但如果您要发布答案,我将不胜感激任何关于如何以这种方式访问​​嵌套元素的解释。

谢谢

您的代码中的问题在 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);
    }

}