Question

I have an XML file that is generated by another company (therefore I can't change the structure of the XML). I need to import portions of the XML file into various different objects. I have used this website to generate the XSD's for the portions of the file that I am interested in.

My problem I am having is that when I execute the code to deserialize the data into an object i receive this error: (updated xsd and now receive the error message below)

"UserSuppliedSection xmlns='http://wgs.thomsonreuters.com/clear/api/report/person-report/niem/1.0'> was not expected."

I have checked this position in my XML file and it represents the beginning of the node I am attempting to extract.

Here is a portion of the 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>

Here is the XSD that was generated: (I added the new elements PersonReportDetails, SectionResults and 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>

Here is the object I am attempting to serialize the data into:

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;
            }
        }
    }
}

And here is the code where I attempt to pull the data into the object:

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();
    }

Any help with this will be most appreciated. Thanks! (xsd and object updated)

Was it helpful?

Solution

Whether this is your essential problem or just an incidental omission is unclear, but your XML is invalid wrt to your XSD: The root element, PersonReportDetails, is defined nowhere in the XSD. Neither is SectionName, for that matter. There are more missing elements, in fact. Please either confirm that fixing the XML solves your problem or update the question with the true (properly pruned, please) XML and XSD.

BTW, the dozens of unused namespace prefix declarations are unnecessary clutter but innocuous at least.


Update per OP's updates:

Still, even with your updates, the XML is not valid with respect to the XSD. You can see by simple inspection that the elements of the XML are in namespaces, and the XSD has nothing about namespaces whatsoever.

Strong recommendation: Before even attempting to generate classes from your XSD, be certain that your XML is valid with respect to your XSD. If you're having trouble with the online XSD generator, try a different tool, or write the XSD manually. Then validate using a known reliable validator such as those based upon Xerces. Only after you receive no validation errors should you expect to be able to have success with the generation of C# classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top