簡體   English   中英

將復雜的XML反序列化為對象

[英]Deserialize Complicated XML into Objects

我有另一個公司生成的XML文件(因此我無法更改XML的結構)。 我需要將XML文件的一部分導入各種不同的對象。 我已經使用該網站為我感興趣的文件部分生成了XSD。

我遇到的問題是,當我執行代碼將數據反序列化為對象時,我收到此錯誤:(更新了xsd,現在收到以下錯誤消息)

“不期望UserSuppliedSection xmlns =' http: //wgs.thomsonreuters.com/clear/api/report/person-report/niem/1.0'>。”

我已經在XML文件中檢查了此位置,它代表了我嘗試提取的節點的開始。

這是XML的一部分

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:PersonReportDetails xmlns:ns2="http://wgs.thomsonreuters.com/clear/api/report/1.0">
  <SectionResults xmlns:ns2="http://niem.gov/niem/structures/2.0" xmlns:ns3="http://niem.gov/niem/niem-core/2.0" xmlns:ns4="http://wgs.thomsonreuters.com/clear/api/search/court-criminal-search-extension/niem/1.0" xmlns:ns5="http://wgs.thomsonreuters.com/clear/api/search/court-search-extension/niem/1.0" xmlns:ns6="http://wgs.thomsonreuters.com/clear/api/search/court-search/niem/1.0" xmlns:ns7="http://wgs.thomsonreuters.com/clear/api/report/report-crime-extension/niem/1.0" xmlns:ns8="http://wgs.thomsonreuters.com/clear/api/report/person-report-extension/niem/1.0" xmlns:ns9="http://wgs.thomsonreuters.com/clear/api/search/sanction-search-extension/niem/1.0" xmlns:ns10="http://wgs.thomsonreuters.com/clear/api/report/report-npi-extension/niem/1.0" xmlns:ns11="http://wgs.thomsonreuters.com/clear/api/search/realproperty-search-extension/niem/1.0" xmlns:ns12="http://wgs.thomsonreuters.com/clear/api/search/realproperty-search/niem/1.0" xmlns:ns13="http://wgs.thomsonreuters.com/clear/api/search/realproperty-asset-extension/niem/1.0" xmlns:ns14="http://wgs.thomsonreuters.com/clear/api/search/court-bankruptcy-search-extension/niem/1.0" xmlns:ns15="http://wgs.thomsonreuters.com/clear/api/search/watercraft-search-extension/niem/1.0" xmlns:ns16="http://wgs.thomsonreuters.com/clear/api/search/watercraft-search/niem/1.0" xmlns:ns17="http://wgs.thomsonreuters.com/clear/api/search/vehicle-dmv-search-extension/niem/1.0" xmlns:ns18="http://wgs.thomsonreuters.com/clear/api/search/vehicle-search-extension/niem/1.0" xmlns:ns19="http://wgs.thomsonreuters.com/clear/api/search/vehicle-search/niem/1.0" xmlns:ns20="http://wgs.thomsonreuters.com/clear/api/report/report-docket-extension/niem/1.0" xmlns:ns21="http://wgs.thomsonreuters.com/clear/api/search/realtimearrest-search-extension/niem/1.0" xmlns:ns22="http://wgs.thomsonreuters.com/clear/api/search/phone-search-extension/niem/1.0" xmlns:ns23="http://wgs.thomsonreuters.com/clear/api/report/report-business-commerce-extension/niem/1.0" xmlns:ns24="http://wgs.thomsonreuters.com/clear/api/report/report-realtimearrest-extension/niem/1.0" xmlns:ns25="http://wgs.thomsonreuters.com/clear/api/search/npi-search-extension/niem/1.0" xmlns:ns26="http://wgs.thomsonreuters.com/clear/api/report/report-personaldata-extension/niem/1.0" xmlns:ns27="http://wgs.thomsonreuters.com/clear/api/search/court-lienjudge-search-extension/niem/1.0" xmlns:ns28="http://wgs.thomsonreuters.com/clear/api/search/license-prolicense-search-extension/niem/1.0" xmlns:ns29="http://wgs.thomsonreuters.com/clear/api/search/license-search-extension/niem/1.0" xmlns:ns30="http://wgs.thomsonreuters.com/clear/api/search/license-search/niem/1.0" xmlns:ns31="http://wgs.thomsonreuters.com/clear/api/search/business-search-extension/niem/1.0" xmlns:ns32="http://wgs.thomsonreuters.com/clear/api/report/report-lienjudge-extension/niem/1.0" xmlns:ns33="http://wgs.thomsonreuters.com/clear/api/report/report-lawsuit-extension/niem/1.0" xmlns:ns34="http://wgs.thomsonreuters.com/clear/api/search/watercraft-vessel-search-extension/niem/1.0" xmlns:ns35="http://wgs.thomsonreuters.com/clear/api/search/workaff-search-extension/niem/1.0" xmlns:ns36="http://wgs.thomsonreuters.com/clear/api/report/report-realProperty-extension/niem/1.0" xmlns:ns37="http://wgs.thomsonreuters.com/clear/api/report/report-driver-vehicle-extension/niem/1.0" xmlns:ns38="http://wgs.thomsonreuters.com/clear/api/search/court-lawsuits-search-extension/niem/1.0" xmlns:ns39="http://wgs.thomsonreuters.com/clear/api/report/report-asset-extension/niem/1.0" xmlns:ns40="http://wgs.thomsonreuters.com/clear/api/report/report-fraud-extension/niem/1.0" xmlns:ns41="http://wgs.thomsonreuters.com/clear/api/search/license-drivers-search-extension/niem/1.0" xmlns:ns42="http://wgs.thomsonreuters.com/clear/api/report/report-sanction-extension/niem/1.0" xmlns:ns43="http://wgs.thomsonreuters.com/clear/api/search/court-ucc-search-extension/niem/1.0" xmlns:ns44="http://wgs.thomsonreuters.com/clear/api/report/report-bankruptcy-extension/niem/1.0" xmlns:ns45="http://wgs.thomsonreuters.com/clear/api/report/report-license-extension/niem/1.0" xmlns:ns46="http://wgs.thomsonreuters.com/clear/api/report/report-workaff-extension/niem/1.0" xmlns:ns47="http://wgs.thomsonreuters.com/clear/api/search/person-search-extension/niem/1.0" xmlns:ns48="http://wgs.thomsonreuters.com/clear/api/search/vehicle-realtime-search-extension/niem/1.0" xmlns:ns49="http://niem.gov/niem/appinfo/2.0" xmlns:ns50="http://wgs.thomsonreuters.com/clear/api/report/1.0" xmlns:ns51="http://wgs.thomsonreuters.com/clear/api/report/person-report/niem/1.0" xmlns:ns52="http://wgs.thomsonreuters.com/clear/api/report/report-asset/niem/1.0" xmlns:ns53="http://wgs.thomsonreuters.com/clear/api/report/report-bankruptcy/niem/1.0" xmlns:ns54="http://wgs.thomsonreuters.com/clear/api/report/report-business-commerce/niem/1.0" xmlns:ns55="http://wgs.thomsonreuters.com/clear/api/report/report-crime/niem/1.0" xmlns:ns56="http://wgs.thomsonreuters.com/clear/api/report/report-docket/niem/1.0" xmlns:ns57="http://wgs.thomsonreuters.com/clear/api/report/report-driver-vehicle/niem/1.0" xmlns:ns58="http://wgs.thomsonreuters.com/clear/api/report/report-fraud/niem/1.0" xmlns:ns59="http://wgs.thomsonreuters.com/clear/api/report/report-lawsuit/niem/1.0" xmlns:ns60="http://wgs.thomsonreuters.com/clear/api/report/report-license/niem/1.0" xmlns:ns61="http://wgs.thomsonreuters.com/clear/api/report/report-lienjudge/niem/1.0" xmlns:ns62="http://wgs.thomsonreuters.com/clear/api/report/report-personaldata/niem/1.0" xmlns:ns63="http://wgs.thomsonreuters.com/clear/api/report/report-realProperty/niem/1.0" xmlns:ns64="http://wgs.thomsonreuters.com/clear/api/search/person-search/niem/1.0" xmlns:ns65="http://wgs.thomsonreuters.com/clear/api/search/phone-search/niem/1.0" xmlns:ns66="http://wgs.thomsonreuters.com/clear/api/search/business-search/niem/1.0">
    <SectionName>UserSuppliedSection</SectionName>
    <CLEARReportDescription>User-Supplied Information</CLEARReportDescription>
    <SectionStatus>COMPLETE</SectionStatus>
    <SectionRecordCount>1</SectionRecordCount>
    <SectionDetails>
      <ns51:UserSuppliedSection>
        <ns8:Address>
          <ns3:LocationCityName>SOMEWHERE</ns3:LocationCityName>
          <ns3:LocationPostalCode>99999</ns3:LocationPostalCode>
          <ns3:LocationStateUSPostalServiceCode>AW</ns3:LocationStateUSPostalServiceCode>
          <ns3:StreetFullText>1234 PENNY LANE</ns3:StreetFullText>
        </ns8:Address>
        <ns8:PersonInfo>
          <ns3:PersonBirthDate>
            <ns3:Date>1776-01--1111111111</ns3:Date>
          </ns3:PersonBirthDate>
          <ns3:PersonName>
            <ns3:PersonGivenName>JOHN</ns3:PersonGivenName>
            <ns3:PersonSurName>DOE</ns3:PersonSurName>
          </ns3:PersonName>
          <ns3:PersonSSNIdentification>
            <ns3:IdentificationID>99999XXXX</ns3:IdentificationID>
          </ns3:PersonSSNIdentification>
        </ns8:PersonInfo>
      </ns51:UserSuppliedSection>
    </SectionDetails>
  </SectionResults>
</ns2:PersonReportDetails>

這是生成的XSD :(我添加了新元素PersonReportDetails,SectionResults和SectionDetails)

<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="PersonReportDetails">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="SectionResults">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="SectionDetails">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="UserSuppliedSection">
                        <xsd:complexType>
                          <xsd:sequence>
                            <xsd:element name="Address">
                            <xsd:complexType>
                              <xsd:sequence>
                                <xsd:element name="LocationCityName" type="xsd:string" />
                                <xsd:element name="LocationPostalCode" type="xsd:int" />
                                <xsd:element name="LocationStateUSPostalServiceCode" type="xsd:string" />
                                <xsd:element name="StreetFullText" type="xsd:string" />
                              </xsd:sequence>
                            </xsd:complexType>
                            </xsd:element>
                            <xsd:element name="PersonInfo">
                            <xsd:complexType>
                              <xsd:sequence>
                                <xsd:element name="PersonBirthDate">
                                <xsd:complexType>
                                  <xsd:sequence>
                                    <xsd:element name="Date" type="xsd:string" />
                                  </xsd:sequence>
                                </xsd:complexType>
                                </xsd:element>
                                <xsd:element name="PersonName">
                                <xsd:complexType>
                                  <xsd:sequence>
                                    <xsd:element name="PersonGivenName" type="xsd:string" />
                                    <xsd:element name="PersonMiddleName" type="xsd:string" />
                                    <xsd:element name="PersonSurName" type="xsd:string" />
                                  </xsd:sequence>
                                </xsd:complexType>
                                </xsd:element>
                                <xsd:element name="PersonSSNIdentification">
                                <xsd:complexType>
                                  <xsd:sequence>
                                    <xsd:element name="IdentificationID" type="xsd:string" />
                                  </xsd:sequence>
                                </xsd:complexType>
                                </xsd:element>
                              </xsd:sequence>
                            </xsd:complexType>
                            </xsd:element>
                          </xsd:sequence>
                        </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
</xsd:element>
</xsd:schema>

這是我嘗試將數據序列化為的對象:

namespace ClearXMLParser.Clear
{
    using System;
    using System.Diagnostics;
    using System.Xml.Serialization;
    using System.Collections;
    using System.Xml.Schema;
    using System.ComponentModel;
    using System.Collections.Generic;


    public partial class PersonReportDetails
    {

        private PersonReportDetailsSectionResults sectionResultsField;

        public PersonReportDetails()
        {
            this.sectionResultsField = new PersonReportDetailsSectionResults();
        }

        public PersonReportDetailsSectionResults SectionResults
        {
            get
            {
                return this.sectionResultsField;
            }
            set
            {
                this.sectionResultsField = value;
            }
        }
    }

    public partial class PersonReportDetailsSectionResults
    {

        private PersonReportDetailsSectionResultsSectionDetails sectionDetailsField;

        public PersonReportDetailsSectionResults()
        {
            this.sectionDetailsField = new PersonReportDetailsSectionResultsSectionDetails();
        }

        public PersonReportDetailsSectionResultsSectionDetails SectionDetails
        {
            get
            {
                return this.sectionDetailsField;
            }
            set
            {
                this.sectionDetailsField = value;
            }
        }
    }

    public partial class PersonReportDetailsSectionResultsSectionDetails
    {

        private PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSection userSuppliedSectionField;

        public PersonReportDetailsSectionResultsSectionDetails()
        {
            this.userSuppliedSectionField = new PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSection();
        }

        public PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSection UserSuppliedSection
        {
            get
            {
                return this.userSuppliedSectionField;
            }
            set
            {
                this.userSuppliedSectionField = value;
            }
        }
    }

    public partial class PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSection
    {

        private PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionAddress addressField;

        private PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfo personInfoField;

        public PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSection()
        {
            this.personInfoField = new PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfo();
            this.addressField = new PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionAddress();
        }

        public PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionAddress Address
        {
            get
            {
                return this.addressField;
            }
            set
            {
                this.addressField = value;
            }
        }

        public PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfo PersonInfo
        {
            get
            {
                return this.personInfoField;
            }
            set
            {
                this.personInfoField = value;
            }
        }
    }

    public partial class PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionAddress
    {

        private string locationCityNameField;

        private int locationPostalCodeField;

        private string locationStateUSPostalServiceCodeField;

        private string streetFullTextField;

        public string LocationCityName
        {
            get
            {
                return this.locationCityNameField;
            }
            set
            {
                this.locationCityNameField = value;
            }
        }

        public int LocationPostalCode
        {
            get
            {
                return this.locationPostalCodeField;
            }
            set
            {
                this.locationPostalCodeField = value;
            }
        }

        public string LocationStateUSPostalServiceCode
        {
            get
            {
                return this.locationStateUSPostalServiceCodeField;
            }
            set
            {
                this.locationStateUSPostalServiceCodeField = value;
            }
        }

        public string StreetFullText
        {
            get
            {
                return this.streetFullTextField;
            }
            set
            {
                this.streetFullTextField = value;
            }
        }
    }

    public partial class PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfo
    {

        private PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfoPersonBirthDate personBirthDateField;

        private PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfoPersonName personNameField;

        private PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfoPersonSSNIdentification personSSNIdentificationField;

        public PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfo()
        {
            this.personSSNIdentificationField = new PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfoPersonSSNIdentification();
            this.personNameField = new PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfoPersonName();
            this.personBirthDateField = new PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfoPersonBirthDate();
        }

        public PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfoPersonBirthDate PersonBirthDate
        {
            get
            {
                return this.personBirthDateField;
            }
            set
            {
                this.personBirthDateField = value;
            }
        }

        public PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfoPersonName PersonName
        {
            get
            {
                return this.personNameField;
            }
            set
            {
                this.personNameField = value;
            }
        }

        public PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfoPersonSSNIdentification PersonSSNIdentification
        {
            get
            {
                return this.personSSNIdentificationField;
            }
            set
            {
                this.personSSNIdentificationField = value;
            }
        }
    }

    public partial class PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfoPersonBirthDate
    {

        private string dateField;

        public string Date
        {
            get
            {
                return this.dateField;
            }
            set
            {
                this.dateField = value;
            }
        }
    }

    public partial class PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfoPersonName
    {

        private string personGivenNameField;

        private string personMiddleNameField;

        private string personSurNameField;

        public string PersonGivenName
        {
            get
            {
                return this.personGivenNameField;
            }
            set
            {
                this.personGivenNameField = value;
            }
        }

        public string PersonMiddleName
        {
            get
            {
                return this.personMiddleNameField;
            }
            set
            {
                this.personMiddleNameField = value;
            }
        }

        public string PersonSurName
        {
            get
            {
                return this.personSurNameField;
            }
            set
            {
                this.personSurNameField = value;
            }
        }
    }

    public partial class PersonReportDetailsSectionResultsSectionDetailsUserSuppliedSectionPersonInfoPersonSSNIdentification
    {

        private string identificationIDField;

        public string IdentificationID
        {
            get
            {
                return this.identificationIDField;
            }
            set
            {
                this.identificationIDField = value;
            }
        }
    }
}

這是我嘗試將數據拉入對象的代碼:

class Program
{
    static void Main(string[] args)
    {
        string xmlFile = @"\\sql01\d$\ImportClear\reports\999-99-9999.xml";
        System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(ClearXMLParser.PersonReportDetails.UserSuppliedSection));
        ClearXMLParser.PersonReportDetails.UserSuppliedSection userSupplied;
        System.IO.FileStream fs = new System.IO.FileStream(xmlFile, System.IO.FileMode.Open);

        using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(fs))
        {
            reader.MoveToContent();
            reader.ReadToDescendant("ns51:UserSuppliedSection");
            userSupplied = (ClearXMLParser.PersonReportDetails.UserSuppliedSection)ser.Deserialize(reader);
        }

        fs.Close();
        fs.Dispose();
    }

任何幫助,將不勝感激。 謝謝! (xsd和對象已更新)

目前尚不清楚這是您的基本問題還是只是偶然的遺漏,但是XML對XSD無效:XSD的任何地方都沒有定義根元素PersonReportDetails 為此, SectionName也不是。 實際上,還有更多的缺失要素。 請確認修復XML可以解決您的問題,或者使用真實的(請適當修剪)XML和XSD更新問題。

順便說一句,數十個未使用的名稱空間前綴聲明是不必要的混亂,但至少是無害的。


每個OP更新的更新:

盡管如此,即使進行了更新, XML相對於XSD還是無效的 通過簡單的檢查,您可以看到XML的元素在名稱空間中,而XSD與名稱空間無關。

強烈建議:甚至在嘗試從XSD生成類之前,請確保XML相對於XSD是有效的。 如果您在使用在線XSD生成器時遇到問題,請嘗試使用其他工具或手動編寫XSD。 然后使用已知可靠的驗證器(例如基於Xerces的驗證器)進行驗證。 只有在沒有收到驗證錯誤之后,您才期望能夠成功生成C#類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM