在c#中访问XML节点

时间:2015-02-19 21:28:39

标签: c# xml

我试图访问nc:StructuredAddress,前面的ecf:CaseParticipantRole =' Debtor'。不确定是否总是有尾随空格,因为这是示例XML,所以我需要适应尾随空格。随后,我还想访问该ecf:CaseParticipantRole的相关nc:LocationCityName,nc:LocationStateUsPostalServiceCode和nc:LocationPostalCode。

我尝试过使用SelectNodes并使用GetElementsByTagName和开头的变体 - (text()=' Debtor']"。我不确定我是否可以甚至在text()子句之后添加更多的节点名称。无论如何......没有任何好运。有什么建议吗?我很少接触XML,所以如果我做一些愚蠢的话,请保持温和。

这是XML。

    <ebn:EBNBatch xmlns="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CoreFilingMessage-4.0" 
          xmlns:ebn="http://ebn.uscourts.gov/EBN-BankruptcyCase-1.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://ebn.uscourts.gov/EBN-BankruptcyCase-1.0 http://ebn.uscourts.gov/xsd/EBN-BankruptcyCase-1.0.xsd" 
          xmlns:ecf="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CommonTypes-4.0" 
          xmlns:bankruptcy="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:BankruptcyCase-4.0" 
          xmlns:j="http://niem.gov/niem/domains/jxdm/4.0" 
          xmlns:nc="http://niem.gov/niem/niem-core/2.0" xmlns:s="http://niem.gov/niem/structures/2.0" >

<nc:DocumentIdentification>
  <nc:IdentificationID>21-573-104452-1 </nc:IdentificationID>
</nc:DocumentIdentification>
<nc:DocumentPostDate>
  <nc:DateTime>2014-07-21T16:27:17 </nc:DateTime>
</nc:DocumentPostDate>
<ecf:SendingMDELocationID>
  <nc:IdentificationID>BNC </nc:IdentificationID>
</ecf:SendingMDELocationID>
<ecf:SendingMDEProfileCode>PDF_Embedded_XML</ecf:SendingMDEProfileCode>
<ebn:BankruptcyCase>

  <ebn:BankruptcyFilingDate>
    <nc:Date>2014-07-21T00:00:00 </nc:Date>
  </ebn:BankruptcyFilingDate>
  <nc:CaseTitleText>Olive Oil</nc:CaseTitleText>
  <nc:CaseCategoryText>BK</nc:CaseCategoryText>
  <nc:CaseDocketID>99-12345</nc:CaseDocketID>
  <j:CaseAugmentation>
    <j:CaseCourt>
      <nc:OrganizationIdentification>
        <nc:IdentificationID>0970-2   </nc:IdentificationID>
      </nc:OrganizationIdentification>
      <nc:OrganizationLocation>
        <nc:LocationAddress>
          <nc:StructuredAddress>
            <nc:LocationStreet>
              <nc:StreetFullText>230 N 1ST AVE      </nc:StreetFullText>
              <nc:StreetFullText>STE 101      </nc:StreetFullText>
            </nc:LocationStreet>
            <nc:LocationCityName>PHOENIX     </nc:LocationCityName>
            <nc:LocationStateUSPostalServiceCode>AZ     </nc:LocationStateUSPostalServiceCode>
            <nc:LocationPostalCode>85003-0608     </nc:LocationPostalCode>
          </nc:StructuredAddress>
        </nc:LocationAddress>
      </nc:OrganizationLocation>
      <nc:OrganizationName>United States Bankruptcy Court  </nc:OrganizationName>
      <nc:OrganizationName>District of Arizona  </nc:OrganizationName>
      <nc:OrganizationPrimaryContactInformation>
        <nc:ContactTelephoneNumber>
          <nc:FullTelephoneNumber>
            <nc:TelephoneNumberFullID>6026824000     </nc:TelephoneNumberFullID>
          </nc:FullTelephoneNumber>
        </nc:ContactTelephoneNumber>
      </nc:OrganizationPrimaryContactInformation>
      <j:CourtName>District of Arizona  </j:CourtName>
    </j:CaseCourt>
    <j:CaseInitiatingParty>
      <nc:EntityPersonReference s:ref="Debtor"/>
    </j:CaseInitiatingParty>
    <j:CaseJudge>
      <nc:RoleOfPersonReference s:ref="Judge"/>
    </j:CaseJudge>
    <j:CaseOtherEntity>
      <nc:EntityPersonReference s:ref="Joint_Debtor"/>
    </j:CaseOtherEntity>
    <j:CaseOtherEntity>
      <nc:EntityPersonReference s:ref="Notice_Recipient"/>
    </j:CaseOtherEntity>
    <j:CaseInitiatingAttorney>
      <nc:RoleOfPersonReference s:ref="Attorney"/>
    </j:CaseInitiatingAttorney>

  </j:CaseAugmentation>
  <ecf:CaseAugmentation>
    <ecf:CaseParticipant>
      <nc:EntityPerson s:id="Debtor">
        <nc:PersonName>

          <nc:PersonFullName>Olive Oil  </nc:PersonFullName>
        </nc:PersonName>
      </nc:EntityPerson>

      <ecf:CaseParticipantRoleCode>Debtor  </ecf:CaseParticipantRoleCode>
      <nc:ContactInformation>
        <nc:ContactMailingAddress>
          <nc:StructuredAddress>
            <nc:LocationStreet>
              <nc:StreetFullText>
                123 Deserted Rd.
                Black Canyon City, Arizona 85324
              </nc:StreetFullText>

            </nc:LocationStreet>
            <nc:LocationCityName>BLACK CANYON CITY     </nc:LocationCityName>
            <nc:LocationStateUSPostalServiceCode>AZ     </nc:LocationStateUSPostalServiceCode>
            <nc:LocationPostalCode>85324     </nc:LocationPostalCode>
          </nc:StructuredAddress>
        </nc:ContactMailingAddress>
      </nc:ContactInformation>
    </ecf:CaseParticipant>
    <ecf:CaseParticipant>
      <nc:EntityPerson s:id="Joint_Debtor">
        <nc:PersonName>

          <nc:PersonFullName>Herman Munster  </nc:PersonFullName>
        </nc:PersonName>
      </nc:EntityPerson>

      <ecf:CaseParticipantRoleCode>Joint_Debtor  </ecf:CaseParticipantRoleCode>
      <nc:ContactInformation>
        <nc:ContactMailingAddress>
          <nc:StructuredAddress>
            <nc:LocationStreet>
              <nc:StreetFullText>
                123 Deserted Rd.
                Black Canyon City, Arizona 85324
              </nc:StreetFullText>

            </nc:LocationStreet>
            <nc:LocationCityName>BLACK CANYON CITY     </nc:LocationCityName>
            <nc:LocationStateUSPostalServiceCode>AZ     </nc:LocationStateUSPostalServiceCode>
            <nc:LocationPostalCode>85324     </nc:LocationPostalCode>
          </nc:StructuredAddress>
        </nc:ContactMailingAddress>
      </nc:ContactInformation>
    </ecf:CaseParticipant>
    <ecf:CaseParticipant>
      <ecf:EntityOrganization s:id="Notice_Recipient">

        <nc:OrganizationName>
          MR. TXT11N

        </nc:OrganizationName>
      </ecf:EntityOrganization>

      <ecf:CaseParticipantRoleCode>Notice_Recipient  </ecf:CaseParticipantRoleCode>
      <nc:ContactInformation>
        <nc:ContactMailingAddress>
          <nc:StructuredAddress>
            <nc:LocationStreet>
              <nc:StreetFullText>
                2525 NETWORK PL
              </nc:StreetFullText>

            </nc:LocationStreet>
            <nc:LocationCityName>HERNDON     </nc:LocationCityName>
            <nc:LocationStateUSPostalServiceCode>VA     </nc:LocationStateUSPostalServiceCode>
            <nc:LocationPostalCode>20171-3514     </nc:LocationPostalCode>
          </nc:StructuredAddress>
        </nc:ContactMailingAddress>
      </nc:ContactInformation>
    </ecf:CaseParticipant>
    <ecf:CaseParticipant>
      <ecf:EntityPerson s:id="Judge">

        <nc:PersonOtherIdentification>
          <nc:IdentificationID>RJH   </nc:IdentificationID>
          <nc:IdentificationCategoryText>Initials   </nc:IdentificationCategoryText>
        </nc:PersonOtherIdentification>
      </ecf:EntityPerson>

      <ecf:CaseParticipantRoleCode>Judge  </ecf:CaseParticipantRoleCode>
    </ecf:CaseParticipant>
    <ecf:CaseParticipant>
      <nc:EntityPerson s:id="Attorney">

        <nc:PersonName>
          <nc:PersonFullName>Mickey Mouse   </nc:PersonFullName>
        </nc:PersonName>
      </nc:EntityPerson>

      <ecf:CaseParticipantRoleCode>Attorney  </ecf:CaseParticipantRoleCode>
      <nc:ContactInformation>
        <nc:ContactMailingAddress>
          <nc:StructuredAddress>
            <nc:LocationStreet>
              <nc:StreetFullText>
                6738 E CAVE CREEK RD
              </nc:StreetFullText>

            </nc:LocationStreet>
            <nc:LocationCityName>CAVE CREEK     </nc:LocationCityName>
            <nc:LocationStateUSPostalServiceCode>AZ     </nc:LocationStateUSPostalServiceCode>
            <nc:LocationPostalCode>85331-8640     </nc:LocationPostalCode>
          </nc:StructuredAddress>
        </nc:ContactMailingAddress>
      </nc:ContactInformation>
    </ecf:CaseParticipant>
  </ecf:CaseAugmentation>
  <bankruptcy:AssetNoticeIndicator>false</bankruptcy:AssetNoticeIndicator>
  <bankruptcy:NumberOfCreditorsValueLevelCode/>
  <bankruptcy:EstimatedAssetsValueLevelCode/>
  <bankruptcy:EstimatedDebtsValueLevelCode/>
  <bankruptcy:SmallBusinessIndicator>false</bankruptcy:SmallBusinessIndicator>
  <bankruptcy:Section1121eIndicator>false</bankruptcy:Section1121eIndicator>
  <bankruptcy:DebtorTypeCode/>

  <bankruptcy:JointPetitionIndicator>true</bankruptcy:JointPetitionIndicator>
  <bankruptcy:NatureOfDebtCode/>
  <bankruptcy:VoluntaryIndicator>false</bankruptcy:VoluntaryIndicator>

  <bankruptcy:BankruptcyStatute>
    <j:StatuteCodeIdentification>
      <nc:IdentificationID>7  </nc:IdentificationID>
    </j:StatuteCodeIdentification>
  </bankruptcy:BankruptcyStatute>
  <bankruptcy:Debtor/>

  <ebn:DebtorInfo>
    <nc:EntityPersonReference s:ref="Debtor"/>

  </ebn:DebtorInfo>

  <ebn:DebtorInfo>
    <nc:EntityPersonReference s:ref="Joint_Debtor"/>
  </ebn:DebtorInfo>

  <ebn:BankruptcyFormCodeText></ebn:BankruptcyFormCodeText>
  <ebn:BankruptcyFormCategory>Notice</ebn:BankruptcyFormCategory>
  <ebn:BankruptcyFormNameText>pdf</ebn:BankruptcyFormNameText>
  <ebn:BankruptcyNoticeShortNameText>Order</ebn:BankruptcyNoticeShortNameText>
  <ebn:NoticePageCount>2</ebn:NoticePageCount>
</ebn:BankruptcyCase>

<FilingConfidentialityIndicator>false</FilingConfidentialityIndicator>

<FilingLeadDocument s:id="_notice">

  <nc:DocumentApplicationName>Ghostscript</nc:DocumentApplicationName>
  <nc:DocumentDescriptionText>Bankruptcy Notice</nc:DocumentDescriptionText>
  <nc:DocumentLanguageCode>eng</nc:DocumentLanguageCode>
  <ecf:DocumentMetadata>
    <j:RegisterActionDescriptionText/>
    <ecf:FilingAttorneyID/>
    <ecf:FilingPartyID/>
  </ecf:DocumentMetadata>
  <ecf:DocumentRendition>
    <ecf:DocumentRenditionMetadata>
      <ecf:DocumentAttachment s:id="_.pdf">
        <nc:BinaryDescriptionText>application/gs  </nc:BinaryDescriptionText>
        <nc:BinaryLocationURI/>
      </ecf:DocumentAttachment>
    </ecf:DocumentRenditionMetadata>
  </ecf:DocumentRendition>
</FilingLeadDocument>

这里是我的C#代码,调整了Name1和Address部分,试图获得&#34;&#34;&#34;工作。

        XmlDocument xmlNotice = new XmlDocument();
        XmlNamespaceManager xmlNameSpc = new XmlNamespaceManager(xmlNotice.NameTable);
        xmlNameSpc.AddNamespace("nc", "http://niem.gov/niem/niem-core/2.0");
        xmlNameSpc.AddNamespace("ecf", "urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CommonTypes-4.0");
        XDocument xmlNot = XDocument.Parse(xml);
        XmlNodeList xmlNodes;
        xmlNotice.LoadXml(xml);

    // DocumentId
        string strDocumentId = string.Empty;
        xmlNodes = xmlNotice.GetElementsByTagName("nc:DocumentIdentification");
        for (int i = 0; i <= xmlNodes.Count - 1; i++)
        {
            strDocumentId = xmlNodes[i].ChildNodes.Item(0).InnerText.Trim();
            System.Diagnostics.Debug.WriteLine("Doc Id: " + strDocumentId);
        }
        // Name1
        string strName1 = string.Empty, strName2 = string.Empty, strDebtorAddress = string.Empty;
        xmlNodes = xmlNotice.GetElementsByTagName("nc:EntityPerson");

        for (int i = 0; i <= xmlNodes.Count - 1; i++)
        {
            if (xmlNodes[i].Attributes != null)
            {
                if (xmlNodes[i].Attributes["s:id"].Value.Equals("Debtor"))
                {
                    strName1 = xmlNodes[i].ChildNodes.Item(0).InnerText.Trim();
                    XmlNodeList xnl = xmlNodes[i].SelectNodes("//ContactInformation/ContactMailingAddress/StructuredAddress/LocationStreet/StreetFullText");
                    //strDebtorAddress = xmlNodes[i].SelectNodes("//ContactInformation/ContactMailingAddress/StructuredAddress/LocationStreet/StreetFullText").InnerText.Trim();
                }
                else if (xmlNodes[i].Attributes["s:id"].Value.Equals("Joint_Debtor"))
                    strName2 = xmlNodes[i].ChildNodes.Item(0).InnerText.Trim();
            }
        }
        System.Diagnostics.Debug.WriteLine("Debtor: " + strName1 + " and Joint_Debtor: " + strName2);
        System.Diagnostics.Debug.WriteLine("Debtor Address: " + strDebtorAddress);

        // Chapter
        string strChapter = string.Empty;
        xmlNodes = xmlNotice.GetElementsByTagName("bankruptcy:BankruptcyStatute");
        for (int i = 0; i <= xmlNodes.Count - 1; i++)
        {
            strChapter = xmlNodes[i].ChildNodes.Item(0).InnerText.Trim();
            System.Diagnostics.Debug.WriteLine("Chapter: " + strChapter);
        }

        // Case Number
        string strCaseNumber = string.Empty;
        xmlNodes = xmlNotice.GetElementsByTagName("nc:CaseDocketID");
        for (int i = 0; i <= xmlNodes.Count - 1; i++)
        {
            strCaseNumber = xmlNodes[i].ChildNodes.Item(0).InnerText.Trim();
            System.Diagnostics.Debug.WriteLine("Case #: " + strCaseNumber);
        }

        // Date Filed
        string strDateFiled = string.Empty;
        xmlNodes = xmlNotice.GetElementsByTagName("ebn:BankruptcyFilingDate");
        for (int i = 0; i <= xmlNodes.Count - 1; i++)
        {
            strDateFiled = xmlNodes[i].ChildNodes.Item(0).InnerText.Trim();
            System.Diagnostics.Debug.WriteLine("Date Filed; " + strDateFiled);
        }

        // Received Date
        string strDateReceived = string.Empty;
        xmlNodes = xmlNotice.GetElementsByTagName("nc:DocumentPostDate");
        for (int i = 0; i <= xmlNodes.Count - 1; i++)
        {
            strDateReceived = xmlNodes[i].ChildNodes.Item(0).InnerText.Trim();
            System.Diagnostics.Debug.WriteLine("Date Received: " + strDateReceived);
        }

        // Address
        string strAddress = string.Empty;
        xmlNodes = xmlNotice.GetElementsByTagName("ecf:CaseParticipantRoleCode[starts-with(text(),'Debtor')]/nc:ContactMailingAddress/nc:StructuredAddress/nc:LocationStreet/nc:StreetFullText");
        for (int i = 0; i <= xmlNodes.Count - 1; i++)
        {
            strAddress = xmlNodes[i].ChildNodes.Item(0).InnerText.Trim();
            System.Diagnostics.Debug.WriteLine("Address: " + strAddress);
        }

1 个答案:

答案 0 :(得分:1)

更新:这将为您提供StructuredAddress es:

的列表
XNamespace ecf = "urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CommonTypes-4.0";
XNamespace nc = "http://niem.gov/niem/niem-core/2.0";

var xml = XDocument.Load(@"C:\test.xml");

var y = xml.Descendants(ecf + "CaseParticipant").Where(p => p.Descendants(ecf + "CaseParticipantRoleCode").Any(c => c.Value.ToString().TrimEnd() == "Debtor"));
var x = y.Descendants(nc + "StructuredAddress");