XML不读取特定节点

时间:2013-10-14 16:48:31

标签: c# xml winforms datagridview xmlreader

我需要读出案例编号并将其放入datagridview行。正在选择其他元素,但我无法获取文件路径中每个XML的案例编号(nc:CaseTrackingID)。我遇到的另一个问题是为每个存在的Base64Object抓取nc:DocumentDescriptionText(这个数字的范围可以从一次到正在处理的文件数量不等)。我已经包含了XML示例和用于读取XML的当前代码。任何帮助将不胜感激。

XML:

<?xml version="1.0"?>
    <nc:DocumentDescriptionText s:id="ReviewWorkQueueId">XXX</nc:DocumentDescriptionText>
    <nc:DocumentDescriptionText s:id="ReviewWorkQueue">XXXXXXXXXXXXXXX</nc:DocumentDescriptionText>
    <nc:DocumentIdentification>
      <nc:IdentificationID>6283348</nc:IdentificationID>
    </nc:DocumentIdentification>
    <nc:DocumentPostDate>
      <nc:DateTime>2013-10-12T09:38:15.2266606-04:00</nc:DateTime>
    </nc:DocumentPostDate>
    <nc:DocumentSubmitter>
      <ecf:EntityPerson s:id="REVIEWER">
        <nc:PersonName>
          <nc:PersonGivenName>XXXXXXXXXXXXXXX</nc:PersonGivenName>
          <nc:PersonMiddleName>Z</nc:PersonMiddleName>
          <nc:PersonSurName>XXXXXXXXXXXXXXX</nc:PersonSurName>
          <nc:PersonFullName>XXXXXXXXXXXXXXX</nc:PersonFullName>
        </nc:PersonName>
        <nc:PersonOtherIdentification>
          <nc:IdentificationID>XXXXXXXXXXXXXXX</nc:IdentificationID>
          <nc:IdentificationCategoryText>FLEPORTAL</nc:IdentificationCategoryText>
        </nc:PersonOtherIdentification>
        <nc:PersonOtherIdentification>
          <nc:IdentificationID>XXXXXXXXXXXXXXX</nc:IdentificationID>
          <nc:IdentificationCategoryText>FLEPORTAL_LOGONNAME</nc:IdentificationCategoryText>
        </nc:PersonOtherIdentification>
        <ecf:PersonAugmentation>
          <nc:ContactInformation>
            <nc:ContactEmailID>XXXXXXXXXXXXXXX</nc:ContactEmailID>
          </nc:ContactInformation>
        </ecf:PersonAugmentation>
      </ecf:EntityPerson>
    </nc:DocumentSubmitter>
    <ecf:SendingMDELocationID>
      <nc:IdentificationID>Filing Review MDE</nc:IdentificationID>
    </ecf:SendingMDELocationID>
    <ecf:SendingMDEProfileCode>urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:WebServicesMessaging-2.0</ecf:SendingMDEProfileCode>
    <CoreFilingMessage xmlns="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CoreFilingMessage-4.0">
      <nc:DocumentEffectiveDate>
        <nc:DateTime>2013-10-10T15:07:06.757-04:00</nc:DateTime>
      </nc:DocumentEffectiveDate>
      <nc:DocumentIdentification>
        <nc:IdentificationID>6283348</nc:IdentificationID>
      </nc:DocumentIdentification>
      <nc:DocumentInformationCutOffDate>
        <nc:DateTime>2013-10-10T15:07:06.757-04:00</nc:DateTime>
      </nc:DocumentInformationCutOffDate>
      <nc:DocumentPostDate>
        <nc:DateTime>2013-10-10T15:06:36-04:00</nc:DateTime>
      </nc:DocumentPostDate>
      <nc:DocumentReceivedDate>
        <nc:DateTime>2013-10-10T15:07:06.757-04:00</nc:DateTime>
      </nc:DocumentReceivedDate>
      <nc:DocumentSubmitter>
        <ecf:EntityPerson s:id="FILER">
          <nc:PersonName>
            <nc:PersonGivenName>XXXX</nc:PersonGivenName>
            <nc:PersonMiddleName>XXXX</nc:PersonMiddleName>
            <nc:PersonSurName>XXXX</nc:PersonSurName>
            <nc:PersonFullName>XXXX</nc:PersonFullName>
          </nc:PersonName>
          <nc:PersonOtherIdentification>
            <nc:IdentificationID>XXXX</nc:IdentificationID>
            <nc:IdentificationCategoryText>FLEPORTAL</nc:IdentificationCategoryText>
          </nc:PersonOtherIdentification>
          <nc:PersonOtherIdentification>
            <nc:IdentificationID>XXXX</nc:IdentificationID>
            <nc:IdentificationCategoryText>FLEPORTAL_LOGONNAME</nc:IdentificationCategoryText>
          </nc:PersonOtherIdentification>
          <nc:PersonOtherIdentification>
            <nc:IdentificationID>XXXX</nc:IdentificationID>
            <nc:IdentificationCategoryText>BAR_NUMBER</nc:IdentificationCategoryText>
          </nc:PersonOtherIdentification>
          <nc:PersonOtherIdentification>
            <nc:IdentificationID>Unaffiliated Users</nc:IdentificationID>
            <nc:IdentificationCategoryText>EMPLOYER</nc:IdentificationCategoryText>
          </nc:PersonOtherIdentification>
          <ecf:PersonAugmentation>
            <nc:ContactInformation>
              <nc:ContactEmailID>XXXX</nc:ContactEmailID>
              <nc:ContactMailingAddress>
                <nc:StructuredAddress>
                  <nc:AddressDeliveryPointText>XXXX</nc:AddressDeliveryPointText>
                  <nc:LocationCityName>XXXX</nc:LocationCityName>
                  <nc:LocationStateUSPostalServiceCode>XX</nc:LocationStateUSPostalServiceCode>
                  <nc:LocationStateName>XX</nc:LocationStateName>
                </nc:StructuredAddress>
                <nc:AddressFullText>XXXX</nc:AddressFullText>
              </nc:ContactMailingAddress>
              <nc:ContactTelephoneNumber s:id="PRIMARYPHONE">
                <nc:FullTelephoneNumber>
                  <nc:TelephoneNumberFullID>XXXX</nc:TelephoneNumberFullID>
                </nc:FullTelephoneNumber>
              </nc:ContactTelephoneNumber>
            </nc:ContactInformation>
          </ecf:PersonAugmentation>
        </ecf:EntityPerson>
      </nc:DocumentSubmitter>
      <ecf:SendingMDELocationID s:id="Filing Assembly MDE">
        <nc:IdentificationID>XXXX</nc:IdentificationID>
        <nc:IdentificationID>XXXX</nc:IdentificationID>
        <nc:IdentificationCategoryText>FLEPORTAL</nc:IdentificationCategoryText>
      </ecf:SendingMDELocationID>
      <ecf:SendingMDEProfileCode>urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:WebServicesMessaging-2.0</ecf:SendingMDEProfileCode>
      <civil:Case>
        <nc:ActivityDescriptionText s:id="CIRCUIT CIVIL">FCL-NON-HOMESTD 50,001-249,999</nc:ActivityDescriptionText>
        <nc:ActivityStatus>
          <nc:StatusText>ORIGINAL PEND                           </nc:StatusText>
          <nc:StatusDate>
            <nc:DateTime>2013-10-10T00:00:00-04:00</nc:DateTime>
          </nc:StatusDate>
        </nc:ActivityStatus>
        <nc:CaseTitleText>XXXXXXXXXXXXX</nc:CaseTitleText>
        <nc:CaseCategoryText s:id="8768812">FCH</nc:CaseCategoryText>
        **<nc:CaseTrackingID>XXXXXXXXXX</nc:CaseTrackingID>**
        <nc:CaseTrackingID s:id="ucn">XXXXXXXXXX</nc:CaseTrackingID>
        <j:CaseAugmentation>
          <j:CaseCourt>
            <nc:OrganizationIdentification>
              <nc:IdentificationID>5</nc:IdentificationID>
              <nc:IdentificationCategoryText>FLEPORTAL_ORGANIZATION</nc:IdentificationCategoryText>
            </nc:OrganizationIdentification>
            <nc:OrganizationIdentification>
              <nc:IdentificationID>1</nc:IdentificationID>
              <nc:IdentificationCategoryText>FLEPORTAL_ORGANIZATION_UNIT</nc:IdentificationCategoryText>
            </nc:OrganizationIdentification>
            <nc:OrganizationIdentification>
              <nc:IdentificationID>Trial</nc:IdentificationID>
              <nc:IdentificationCategoryText>XXXXXXX</nc:IdentificationCategoryText>
            </nc:OrganizationIdentification>
            <nc:OrganizationIdentification>
              <nc:IdentificationID>XXXXXXX</nc:IdentificationID>
              <nc:IdentificationCategoryText>XXXXXXX</nc:IdentificationCategoryText>
            </nc:OrganizationIdentification>
            <nc:OrganizationName>XXXXXXX</nc:OrganizationName>
            <nc:OrganizationUnitName>XXXXXXX</nc:OrganizationUnitName>
            <j:CourtName>XXXXXXX</j:CourtName>
          </j:CaseCourt>
        </j:CaseAugmentation>
        <ecf:CaseAugmentation>
          <nc:LanguageCode xsi:nil="true" />
        </ecf:CaseAugmentation>
      </civil:Case>
      <FilingConfidentialityIndicator>false</FilingConfidentialityIndicator>
      <FilingLeadDocument s:id="DOC00001" s:metadata="# Pages=2">
        <nc:DocumentApplicationName>application/pdf</nc:DocumentApplicationName>
        **<nc:DocumentDescriptionText s:id="Affidavits and Oaths">Affidavit:  Military Status Unknown</nc:DocumentDescriptionText>**

C#代码:

private void Form1_Load(object sender, EventArgs e)
{
    string sourceDir = @"\\filepath of XML";
    //var pattern = "*.xml";

    XmlReaderSettings settings = new XmlReaderSettings();
    settings.IgnoreComments = true;
    settings.IgnoreProcessingInstructions = true;
    settings.IgnoreWhitespace = true;
    settings.CheckCharacters = false;


    string DocID = null;
    int elementCount = 0;
    string reqID = null;
    string attyName = null;
    string attyBarID = null;
    //string reqDateTime = null;
    string empName = null;

    // Reads only XML files from the specific folder
    string[] fileEntries = Directory.GetFiles(sourceDir, "*.xml");

    foreach (string fileName in fileEntries)
    {
        DateTime dt = File.GetLastWriteTime(fileName);
        dt.ToString("yyyy-MM-dd HH:mm:ss");

            XmlReader xr = XmlReader.Create(fileName, settings); //reads XML from folder
            {
                while (xr.Read())
                {
                    if (xr.NodeType == XmlNodeType.Element && xr.Name == "nc:PersonFullName")
                    {
                        attyName = xr.ReadElementContentAsString();
                    }

                    if (xr.NodeType == XmlNodeType.Element && xr.Name == "nc:IdentificationID")
                    {
                        elementCount++;
                        DocID = xr.ReadElementContentAsString();
                        if (elementCount == 1)
                        {
                            reqID = DocID;
                        }

                        if (elementCount == 3)
                        {
                            empName = DocID;
                        }

                        if (elementCount == 8)
                        {
                            attyBarID = DocID;

                            elementCount = 0;
                             break;
                        }
                    }

                    if (xr.NodeType == XmlNodeType.Element && xr.Name == "nc:CaseTrackingID")
                        {
                            caseIDCount++;
                            CaseID = xr.ReadElementContentAsString();
                            if (caseIDCount == 3)
                            {
                                casenumber = CaseID;

                                caseIDCount = 0;
                                break;
                            }
                        }

                        int StartPos; // Current pos in file.

                        StreamReader sr = new StreamReader(fileName);
                        String Str = sr.ReadToEnd();

                        int Count = 0;
                        StartPos = 0;
                        do
                        {
                            StartPos = Str.IndexOf("<nc:BinaryBase64Object>", StartPos);
                            if (StartPos >= 0)
                            {
                                StartPos++;
                                Count++;
                            }
                        } while (StartPos >= 0);
                        int index = dataGridView1.Rows.Add();

                        DataGridViewRow row = dataGridView1.Rows[index];
                        dataGridView1.Sort(dataGridView1.Columns["datetime_completed"], ListSortDirection.Ascending);

                        row.Cells["trans_nbr"].Value = reqID;
                        row.Cells["user_completed"].Value = empName;
                        row.Cells["datetime_completed"].Value = dt;
                        row.Cells["atty_bar_no"].Value = attyBarID;
                        row.Cells["attorney_name"].Value = attyName;
                        row.Cells["doc_count"].Value = Count;
                        row.Cells["CaseNumber"].Value = casenumber;
               }
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

看起来CaseTrackingID标签周围有**。你试过从XML文件中删除double *吗?我不确定**的意义是什么,但可能它干扰了阅读文件。

**<nc:CaseTrackingID>XXXXXXXXXX</nc:CaseTrackingID>**

这看起来像个问题。不确定**是否需要存在。

修改

如何改变这样的代码呢。这将在遍历节点时捕获文本节点

if (xr.NodeType == XmlNodeType.Element)
{
    element = xr.Name;
}
else if (xr.NodeType == XmlNodeType.Text)
{
    if (element == "nc:PersonFullName")
    {
        attyName = xr.Value;
    }
    else if (element == "nc:IdentificationID")
    {
        elementCount++;
        DocID = xr.Value;
        if (elementCount == 1)
        {
            reqID = DocID;
        }

        if (elementCount == 3)
        {
            empName = DocID;
        }

        if (elementCount == 8)
        {
            attyBarID = DocID;

            elementCount = 0;
            break;
        }
    }
    else if (element == "nc:CaseTrackingID")
    {
        elementCount++;
        DocID = xr.Value;
        if (elementCount == 1)
        {
            reqID = DocID;
        }

        if (elementCount == 3)
        {
            empName = DocID;
        }

        if (elementCount == 8)
        {
            attyBarID = DocID;

            elementCount = 0;
            break;
        }
    }
}

还将elelment变量放在循环外

答案 1 :(得分:0)

因此,根据我的研究,我使它变得比需要的更复杂。能够直接获取元素比通过“IF”语句运行更有益。我注意到elementCount ++不允许读取下面的元素。

xr.ReadToFollowing("nc:CaseTrackingID");//this will directly get the element name
casenumber = xr.ReadElementContentAsString();