无法使用命名空间获取XElement Descendant元素值

时间:2017-05-17 14:04:06

标签: c# xml xpath linq-to-xml

在下面的代码中,我将Id的值设置为null,即使它位于xml片段中。获得可迭代后,看起来需要更多东西才能获得值。任何帮助非常感谢。

 var str =
  @"
<CustomerCareReport xmlns='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.FireEms' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
  <RecordAction xmlns='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects'>Edit</RecordAction>
  <FireIncident xmlns:a='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.FireIncidents'>
    <a:Address xmlns:b='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.Addresses'>
      <b:City>SomeCity</b:City>
      <b:HouseNumber>4019</b:HouseNumber>
      <b:Id>1651880</b:Id>
      <b:Lat>47.573911198</b:Lat>
      <b:LocationType>Address</b:LocationType>
      <b:Lon>-122.169529554</b:Lon>
      <b:State xmlns:c='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.ValidationSets'>
        <RecordAction xmlns='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects'>Relate</RecordAction>
        <c:Description>Washington</c:Description>
        <c:EntryValue>WA</c:EntryValue>
        <c:FilterString>WA Washington</c:FilterString>
        <c:Id>85</c:Id>
        <c:Inactive>false</c:Inactive>
        <c:SetId>1021</c:SetId>
      </b:State>
      <b:StreetName>FACTORIA</b:StreetName>
      <b:StreetSuffix>SE</b:StreetSuffix>
      <b:StreetType>BLVD</b:StreetType>
      <b:Venue>
        <b:VenueName>BE</b:VenueName>
      </b:Venue>
      <b:VenueName>BE</b:VenueName>
      <b:Zip>98006</b:Zip>
    </a:Address>
    <a:FDID xmlns:b='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.Jurisdictions'>
      <RecordAction xmlns='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects'>Relate</RecordAction>
      <b:Ori>17M02</b:Ori>
    </a:FDID>
    <a:IncidentNumber>2016-00000071</a:IncidentNumber>
  </FireIncident>
  <OtherInformation>
    <RecordAction xmlns='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects'>Add</RecordAction>
    <Documents xmlns:a='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.Documents'>
      <a:Document>
        <RecordAction xmlns='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects'>Add</RecordAction>
        <a:CheckOutMachine>TROPPLT</a:CheckOutMachine>
        <a:Contents>Z2dn</a:Contents>
        <a:Description>yoyo</a:Description>
        <a:FileType xmlns:b='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.ValidationSets'>
          <RecordAction xmlns='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects'>Relate</RecordAction>
          <b:Description>Notepad</b:Description>
          <b:EntryValue>txt</b:EntryValue>
          <b:FilterString>txt Notepad</b:FilterString>
          <b:Id>1</b:Id>
          <b:Inactive>false</b:Inactive>
          <b:SetId>2574</b:SetId>
        </a:FileType>
        <a:Id>0e2821b0-9430-40a8-8964-443cdeeda018</a:Id>
        <a:Name>yoyo</a:Name>
        <a:UsageKey>0</a:UsageKey>
        <a:UsageType>56</a:UsageType>
      </a:Document>
      <a:Document>
        <RecordAction xmlns='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects'>Add</RecordAction>
        <a:CheckOutMachine>TROPPLTBKOLLURU</a:CheckOutMachine>
        <a:Contents>ZmhmaA0KZmZmDQpmamZqZmpmDQo=</a:Contents>
        <a:Description>test 2</a:Description>
        <a:FileType xmlns:b='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.ValidationSets'>
          <RecordAction xmlns='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects'>Relate</RecordAction>
          <b:Description>Notepad</b:Description>
          <b:EntryValue>txt</b:EntryValue>
          <b:FilterString>txt Notepad</b:FilterString>
          <b:Id>1</b:Id>
          <b:Inactive>false</b:Inactive>
          <b:SetId>2574</b:SetId>
        </a:FileType>
        <a:Id>9b42f7e0-e0b3-4e83-a466-5862e9cb5bbe</a:Id>
        <a:Name>test 2</a:Name>
        <a:UsageKey>0</a:UsageKey>
        <a:UsageType>56</a:UsageType>
      </a:Document>
    </Documents>
  </OtherInformation>
  <Response>
    <Apparatus xmlns:a='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.Units'>
      <RecordAction xmlns='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects'>Relate</RecordAction>
      <a:Id>-1672</a:Id>
      <a:UnitNumber>E1</a:UnitNumber>
    </Apparatus>
    <ResponseNumber>1600000071E1</ResponseNumber>
  </Response>
  <Time>
    <CallDatetime>2016-07-01T12:26:34.277-04:00</CallDatetime>
    <DispatchNotifiedDatetime>2016-07-01T12:26:34.277-04:00</DispatchNotifiedDatetime>
  </Time>
</CustomerCareReport>
";

            var xelement = XElement.Parse(str);
            XNamespace ns = xelement.Name.Namespace;
            var documents = xelement.Descendants(ns + "Documents");

            foreach (XElement document in documents)
            {
                if (document.Name.LocalName.Equals("Id"))
                {
                    Console.WriteLine(document.Value);

                }

            }

1 个答案:

答案 0 :(得分:2)

正如评论中所暗示的,Documents的子元素没有本地名称Id。这是孩子a:Document元素的孩子。

使用正确的命名空间,您的查询应如下所示:

XNamespace ns = "http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.FireEms"
XNamespace a = "http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.Documents"

var element = XElement.Parse(str);

var ids = element
    .Descendants(ns + "Documents")
    .Descendants(a + "Id");

有关正常工作的演示,请参阅this fiddle