我有一个由另一家公司生成的XML文件(因此我无法更改XML的结构)。我需要将部分XML文件导入各种不同的对象。我已使用此website为我感兴趣的文件部分生成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和对象更新)
答案 0 :(得分:0)
这是您的基本问题还是偶然遗漏尚不清楚,但您的XML对XSD无效:根元素PersonReportDetails
在XSD中无处定义。就此问题而言,SectionName
都不是。事实上,有更多缺失的元素。请确认修复XML可以解决您的问题,或者使用true(经过适当修剪的)XML和XSD更新问题。
更新每个OP的更新:
尽管如此,即使您的更新, XML对于XSD 也无效。您可以通过简单的检查看到XML的元素在命名空间中,而XSD没有关于命名空间的任何内容。
强烈推荐:在尝试从XSD生成类之前,请确保您的XML对XSD有效。如果您在使用在线XSD生成器时遇到问题,请尝试使用其他工具,或手动编写XSD。然后使用已知的可靠验证器进行验证,例如基于Xerces的验证器。 只有在收到没有验证错误之后,您才能期望能够成功生成C#类。