下面的XML需要反序列化为POCOs。让我们假设XML暂时不能被修改:
<?xml version="1.0" encoding="UTF-8"?>
<query:QueryRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xmime="http://www.w3.org/2005/05/xmlmime"
xmlns:rim="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0"
xmlns:query="urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
id="4ffb5281-179d-4578-adf2-39fd13ccc797">
<rim:Slot name="SpecificationIdentifier">
<rim:SlotValue xsi:type="rim:StringValueType">
<rim:Value>sfts-edm:v1.0</rim:Value>
</rim:SlotValue>
</rim:Slot>
<rim:Slot name="IssueDateTime">
<rim:SlotValue xsi:type="rim:DateTimeValueType">
<rim:Value>2022-05-19T17:08:10.872Z</rim:Value>
</rim:SlotValue>
</rim:Slot>
<rim:Slot name="Procedure">
<rim:SlotValue xsi:type="rim:InternationalStringValueType">
<rim:Value>
<rim:LocalizedString xml:lang="en"
value="Requesting a certificate"/>
</rim:Value>
</rim:SlotValue>
</rim:Slot>
<rim:Slot name="PossibilityForPreview">
<rim:SlotValue xsi:type="rim:BooleanValueType">
<rim:Value>false</rim:Value>
</rim:SlotValue>
</rim:Slot>
<rim:Slot name="ExplicitRequestGiven">
<rim:SlotValue xsi:type="rim:BooleanValueType">
<rim:Value>true</rim:Value>
</rim:SlotValue>
</rim:Slot>
<rim:Slot name="Requirements">
<rim:SlotValue xsi:type="rim:CollectionValueType"
collectionType="urn:oasis:names:tc:ebxml-regrep:CollectionType:Set">
<rim:Element xsi:type="rim:AnyValueType">
<rim:Requirement>
<Identifier>315cfd75-6605-49c4-b0fe-799833b41099</Identifier>
<Name lang="en">Proof of Birth</Name>
</rim:Requirement>
</rim:Element>
</rim:SlotValue>
</rim:Slot>
<rim:Slot name="EvidenceProvider">
<rim:SlotValue xsi:type="rim:AnyValueType">
<Agent>
<Identifier schemeID="9930">DE73524311</Identifier>
<Name>Civil Registration Office Berlin I</Name>
</Agent>
</rim:SlotValue>
</rim:Slot>
<query:ResponseOption returnType="LeafClassWithRepositoryItem"/>
<query:Query queryDefinition="DocumentQuery">
<rim:Slot name="NaturalPerson">
<rim:SlotValue xsi:type="rim:AnyValueType">
<Person>
<LevelOfAssurance>High</LevelOfAssurance>
<Identifier schemeID="eidas">DK/DE/123456</Identifier>
<FamilyName>Smith</FamilyName>
<GivenName>John</GivenName>
<DateOfBirth>1970-03-01</DateOfBirth>
<PlaceOfBirth>Hamburg, Germany</PlaceOfBirth>
<CurrentAddress>
<FullAddress>Dieter Wellhausen 71</FullAddress>
<AdminUnitLevel1>DE</AdminUnitLevel1>
</CurrentAddress>
<Gender>Male</Gender>
</Person>
</rim:SlotValue>
</rim:Slot>
<rim:Slot name="EvidenceRequest">
<rim:SlotValue xsi:type="rim:AnyValueType">
<DataServiceEvidenceType xmlns="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0">
<Identifier>2af27699-f131-4411-8fdb-9e8cd4e8bded</Identifier>
<EvidenceTypeClassification>Certificate</EvidenceTypeClassification>
<Title lang="en">Certificate of Birth</Title>
</DataServiceEvidenceType>
</rim:SlotValue>
</rim:Slot>
</query:Query>
</query:QueryRequest>使用上面的XML,我使用XMLToCSharp创建了以下类
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace TestRequest2
{
[XmlRoot(ElementName="SlotValue", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class SlotValue {
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlElement(ElementName="Value", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Value Value { get; set; }
[XmlElement(ElementName="Element", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Element Element { get; set; }
[XmlAttribute(AttributeName="collectionType")]
public string CollectionType { get; set; }
[XmlElement(ElementName="Agent")]
public Agent Agent { get; set; }
[XmlElement(ElementName="Person")]
public Person Person { get; set; }
[XmlElement(ElementName="DataServiceEvidenceType", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public DataServiceEvidenceType DataServiceEvidenceType { get; set; }
}
[XmlRoot(ElementName="Slot", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Slot {
[XmlElement(ElementName="SlotValue", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public SlotValue SlotValue { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
}
[XmlRoot(ElementName="LocalizedString", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class LocalizedString {
[XmlAttribute(AttributeName="lang", Namespace="http://www.w3.org/XML/1998/namespace")]
public string Lang { get; set; }
[XmlAttribute(AttributeName="value")]
public string Value { get; set; }
}
[XmlRoot(ElementName="Value", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Value {
[XmlElement(ElementName="LocalizedString", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public LocalizedString LocalizedString { get; set; }
}
[XmlRoot(ElementName="Name")]
public class Name {
[XmlAttribute(AttributeName="lang")]
public string Lang { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="Requirement", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Requirement {
[XmlElement(ElementName="Identifier")]
public string Identifier { get; set; }
[XmlElement(ElementName="Name")]
public Name Name { get; set; }
}
[XmlRoot(ElementName="Element", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Element {
[XmlElement(ElementName="Requirement", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Requirement Requirement { get; set; }
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
}
[XmlRoot(ElementName="Identifier")]
public class Identifier {
[XmlAttribute(AttributeName="schemeID")]
public string SchemeID { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="Agent")]
public class Agent {
[XmlElement(ElementName="Identifier")]
public Identifier Identifier { get; set; }
[XmlElement(ElementName="Name")]
public string Name { get; set; }
}
[XmlRoot(ElementName="ResponseOption", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public class ResponseOption {
[XmlAttribute(AttributeName="returnType")]
public string ReturnType { get; set; }
}
[XmlRoot(ElementName="CurrentAddress")]
public class CurrentAddress {
[XmlElement(ElementName="FullAddress")]
public string FullAddress { get; set; }
[XmlElement(ElementName="AdminUnitLevel1")]
public string AdminUnitLevel1 { get; set; }
}
[XmlRoot(ElementName="Person")]
public class Person {
[XmlElement(ElementName="LevelOfAssurance")]
public string LevelOfAssurance { get; set; }
[XmlElement(ElementName="Identifier")]
public Identifier Identifier { get; set; }
[XmlElement(ElementName="FamilyName")]
public string FamilyName { get; set; }
[XmlElement(ElementName="GivenName")]
public string GivenName { get; set; }
[XmlElement(ElementName="DateOfBirth")]
public string DateOfBirth { get; set; }
[XmlElement(ElementName="PlaceOfBirth")]
public string PlaceOfBirth { get; set; }
[XmlElement(ElementName="CurrentAddress")]
public CurrentAddress CurrentAddress { get; set; }
[XmlElement(ElementName="Gender")]
public string Gender { get; set; }
}
[XmlRoot(ElementName="Title", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Title {
[XmlAttribute(AttributeName="lang")]
public string Lang { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="DataServiceEvidenceType", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class DataServiceEvidenceType {
[XmlElement(ElementName="Identifier", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public string Identifier2 { get; set; }
[XmlElement(ElementName="EvidenceTypeClassification", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public string EvidenceTypeClassification { get; set; }
[XmlElement(ElementName="Title", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Title Title { get; set; }
[XmlAttribute(AttributeName="xmlns")]
public string Xmlns { get; set; }
}
[XmlRoot(ElementName="Query", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public class Query {
[XmlElement(ElementName="Slot", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public List<Slot> Slot { get; set; }
[XmlAttribute(AttributeName="queryDefinition")]
public string QueryDefinition { get; set; }
}
[XmlRoot(ElementName="QueryRequest", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public class QueryRequest {
[XmlElement(ElementName="Slot", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public List<Slot> Slot { get; set; }
[XmlElement(ElementName="ResponseOption", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public ResponseOption ResponseOption { get; set; }
[XmlElement(ElementName="Query", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public Query Query { get; set; }
[XmlAttribute(AttributeName="query", Namespace="http://www.w3.org/2000/xmlns/")]
public string _Query { get; set; }
[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName="xmime", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xmime { get; set; }
[XmlAttribute(AttributeName="rim", Namespace="http://www.w3.org/2000/xmlns/")]
public string Rim { get; set; }
[XmlAttribute(AttributeName="xlink", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xlink { get; set; }
[XmlAttribute(AttributeName="xml", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xml { get; set; }
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
}
}当我试图反序列化时,引发了以下错误:
Unhandled exception. System.InvalidOperationException: There is an error in XML document (11, 4).
---> System.InvalidOperationException: The specified type was not recognized: name='StringValueType', namespace='urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0', at <SlotValue xmlns='urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0'>.在进行了一些研究之后,我遇到了这个StackOverflow帖子,它建议将所有可能的xsi:type类型定义为SlotValue的子类,并使用[XmlInclude]属性进行包含。这似乎带来了一些进展。我修改了类SlotValue如下所示:
[XmlRoot(ElementName="SlotValue", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
[XmlInclude(typeof(StringValueType))]
[XmlInclude(typeof(DateTimeValueType))]
[XmlInclude(typeof(InternationalStringValueType))]
[XmlInclude(typeof(BooleanValueType))]
[XmlInclude(typeof(CollectionValueType))]
[XmlInclude(typeof(AnyValueType))]
public class SlotValue {
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlElement(ElementName="Value", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Value Value { get; set; }
[XmlElement(ElementName="Element", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Element Element { get; set; }
[XmlAttribute(AttributeName="collectionType")]
public string CollectionType { get; set; }
[XmlElement(ElementName="Agent")]
public Agent Agent { get; set; }
[XmlElement(ElementName="Person")]
public Person Person { get; set; }
[XmlElement(ElementName="DataServiceEvidenceType", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public DataServiceEvidenceType DataServiceEvidenceType { get; set; }
}
[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "StringValueType")]
public class StringValueType : SlotValue {}
[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "DateTimeValueType")]
public class DateTimeValueType : SlotValue {}
[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "InternationalStringValueType")]
public class InternationalStringValueType : SlotValue {}
[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "BooleanValueType")]
public class BooleanValueType : SlotValue {}
[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "CollectionValueType")]
public class CollectionValueType : SlotValue {}
[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "AnyValueType")]
public class AnyValueType : SlotValue {}但是,在这样做时,将引发相同的异常,这一次将抛出属于另一个元素Element的另一行。
Unhandled exception. System.InvalidOperationException: There is an error in XML document (41, 5).
---> System.InvalidOperationException: The specified type was not recognized: name='AnyValueType', namespace='urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0', at <Element xmlns='urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0'>.以上的类型列表似乎会被多个XML元素使用。在这种情况下,AnyValueType似乎在SlotValue和Element之间共享。到目前为止,我还没有找到解决办法。
我最近的尝试包括将[XmlType]属性放在Element之上,如下所示:
[XmlType(Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0", TypeName = "AnyValueType")]
[XmlRoot(ElementName="Element", Namespace="urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
[XmlInclude(typeof(AnyValueType))]
public class Element {但是,这将与放置在SlotValue上的属性冲突,因为如果我要这样做,将引发以下异常:
---> System.InvalidOperationException: Types 'TestRequest.AnyValueType' and 'TestRequest.Element' both use the XML type name, 'AnyValueType', from namespace 'urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0'. Use XML attributes to
specify a unique XML name and/or namespace for the type.发布于 2022-09-12 14:02:05
谢谢你@jdweng,我设法找到了解决这个问题的办法。
基本上,我删除了类Element并将其属性(在本例中只有一个--因为它是多余的)删除了type到AnyValueType中。然后,我简单地将CollectionValueType中对AnyValueType的调用替换为AnyValueType。以@jdweng为例,它应该如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace ConsoleApplication40
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(QueryRequest));
QueryRequest request = (QueryRequest)serializer.Deserialize(reader);
}
}
[XmlInclude(typeof(AnyValueType))]
[XmlInclude(typeof(StringValueType))]
[XmlInclude(typeof(InternationalStringValueType))]
[XmlInclude(typeof(DateTimeValueType))]
[XmlInclude(typeof(BooleanValueType))]
[XmlInclude(typeof(CollectionValueType))]
[XmlInclude(typeof(Element))]
public class SlotValue
{
[XmlAttribute("name")]
public string name { get; set; }
[XmlElement(ElementName = "Agent")]
public Agent Agent { get; set; }
[XmlElement(ElementName = "Person")]
public Person Person { get; set; }
[XmlElement(ElementName = "DataServiceEvidenceType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public DataServiceEvidenceType DataServiceEvidenceType { get; set; }
}
[XmlRoot(ElementName = "AnyValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class AnyValueType : SlotValue
{
[XmlElement(ElementName = "Requirement", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Requirement Requirement { get; set; }
[XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public object Value { get; set; }
}
[XmlRoot(ElementName = "DateTimeValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class DateTimeValueType : SlotValue
{
[XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public DateTime Value { get; set; }
}
[XmlRoot(ElementName = "StringValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class StringValueType : SlotValue
{
[XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Value Value { get; set; }
}
[XmlRoot(ElementName = "InternationalStringValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class InternationalStringValueType : SlotValue
{
[XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Value Value { get; set; }
}
[XmlRoot(ElementName = "BooleanValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class BooleanValueType : SlotValue
{
[XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Boolean Value { get; set; }
}
[XmlRoot(ElementName = "CollectionValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class CollectionValueType : SlotValue
{
[XmlElement("Element", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public AnyValueType Element { get; set; }
}
[XmlRoot(ElementName = "Slot", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Slot
{
[XmlElement(ElementName = "SlotValue", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public SlotValue SlotValue { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "LocalizedString", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class LocalizedString
{
[XmlAttribute(AttributeName = "lang", Namespace = "http://www.w3.org/XML/1998/namespace")]
public string Lang { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Value
{
[XmlElement(ElementName = "LocalizedString", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public LocalizedString LocalizedString { get; set; }
}
[XmlRoot(ElementName = "Name")]
public class Name
{
[XmlAttribute(AttributeName = "lang")]
public string Lang { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Requirement", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Requirement
{
[XmlElement(ElementName = "Identifier")]
public string Identifier { get; set; }
[XmlElement(ElementName = "Name")]
public Name Name { get; set; }
}
[XmlRoot(ElementName = "Identifier")]
public class Identifier
{
[XmlAttribute(AttributeName = "schemeID")]
public string SchemeID { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Agent")]
public class Agent
{
[XmlElement(ElementName = "Identifier")]
public Identifier Identifier { get; set; }
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "ResponseOption", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public class ResponseOption
{
[XmlAttribute(AttributeName = "returnType")]
public string ReturnType { get; set; }
}
[XmlRoot(ElementName = "CurrentAddress")]
public class CurrentAddress
{
[XmlElement(ElementName = "FullAddress")]
public string FullAddress { get; set; }
[XmlElement(ElementName = "AdminUnitLevel1")]
public string AdminUnitLevel1 { get; set; }
}
[XmlRoot(ElementName = "Person")]
public class Person
{
[XmlElement(ElementName = "LevelOfAssurance")]
public string LevelOfAssurance { get; set; }
[XmlElement(ElementName = "Identifier")]
public Identifier Identifier { get; set; }
[XmlElement(ElementName = "FamilyName")]
public string FamilyName { get; set; }
[XmlElement(ElementName = "GivenName")]
public string GivenName { get; set; }
[XmlElement(ElementName = "DateOfBirth")]
public string DateOfBirth { get; set; }
[XmlElement(ElementName = "PlaceOfBirth")]
public string PlaceOfBirth { get; set; }
[XmlElement(ElementName = "CurrentAddress")]
public CurrentAddress CurrentAddress { get; set; }
[XmlElement(ElementName = "Gender")]
public string Gender { get; set; }
}
[XmlRoot(ElementName = "Title", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Title
{
[XmlAttribute(AttributeName = "lang")]
public string Lang { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "DataServiceEvidenceType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class DataServiceEvidenceType
{
[XmlElement(ElementName = "Identifier", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public string Identifier2 { get; set; }
[XmlElement(ElementName = "EvidenceTypeClassification", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public string EvidenceTypeClassification { get; set; }
[XmlElement(ElementName = "Title", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Title Title { get; set; }
}
[XmlRoot(ElementName = "Query", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public class Query
{
[XmlElement(ElementName = "Slot", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public List<Slot> Slot { get; set; }
[XmlAttribute(AttributeName = "queryDefinition")]
public string QueryDefinition { get; set; }
}
[XmlRoot(ElementName = "QueryRequest", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public class QueryRequest
{
[XmlElement(ElementName = "Slot", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public List<Slot> Slot { get; set; }
[XmlElement(ElementName = "ResponseOption", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public ResponseOption ResponseOption { get; set; }
[XmlElement(ElementName = "Query", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public Query Query { get; set; }
[XmlAttribute(AttributeName = "query", Namespace = "http://www.w3.org/2000/xmlns/")]
public string _Query { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
}
}发布于 2022-09-05 17:47:31
xsi:type属性指示继承的类。XML序列化不易调试。为了隔离问题,我注释掉了类的各个部分。然后修复问题,并将注释代码添加回类中。以下是我所做的改变
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace ConsoleApplication40
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(QueryRequest));
QueryRequest request = (QueryRequest)serializer.Deserialize(reader);
}
}
[XmlInclude(typeof(AnyValueType))]
[XmlInclude(typeof(StringValueType))]
[XmlInclude(typeof(InternationalStringValueType))]
[XmlInclude(typeof(DateTimeValueType))]
[XmlInclude(typeof(BooleanValueType))]
[XmlInclude(typeof(CollectionValueType))]
[XmlInclude(typeof(Element))]
public class SlotValue
{
[XmlAttribute("name")]
public string name { get; set; }
[XmlElement(ElementName = "Agent")]
public Agent Agent { get; set; }
[XmlElement(ElementName = "Person")]
public Person Person { get; set; }
[XmlElement(ElementName = "DataServiceEvidenceType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public DataServiceEvidenceType DataServiceEvidenceType { get; set; }
}
[XmlRoot(ElementName = "AnyValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class AnyValueType : SlotValue
{
[XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public object Value { get; set; }
}
[XmlRoot(ElementName = "DateTimeValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class DateTimeValueType : SlotValue
{
[XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public DateTime Value { get; set; }
}
[XmlRoot(ElementName = "StringValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class StringValueType : SlotValue
{
[XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Value Value { get; set; }
}
[XmlRoot(ElementName = "InternationalStringValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class InternationalStringValueType : SlotValue
{
[XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Value Value { get; set; }
}
[XmlRoot(ElementName = "BooleanValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class BooleanValueType : SlotValue
{
[XmlElement(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Boolean Value { get; set; }
}
[XmlRoot(ElementName = "CollectionValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class CollectionValueType : SlotValue
{
//[XmlElement(ElementName = "Element", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
//public Element Element { get; set; }
}
[XmlRoot(ElementName = "Slot", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Slot
{
[XmlElement(ElementName = "SlotValue", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public SlotValue SlotValue { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "LocalizedString", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class LocalizedString
{
[XmlAttribute(AttributeName = "lang", Namespace = "http://www.w3.org/XML/1998/namespace")]
public string Lang { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "Value", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Value
{
[XmlElement(ElementName = "LocalizedString", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public LocalizedString LocalizedString { get; set; }
}
[XmlRoot(ElementName = "Name")]
public class Name
{
[XmlAttribute(AttributeName = "lang")]
public string Lang { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Requirement", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Requirement
{
[XmlElement(ElementName = "Identifier")]
public string Identifier { get; set; }
[XmlElement(ElementName = "Name")]
public Name Name { get; set; }
}
[XmlInclude(typeof(AnyValueTypeElement))]
[XmlRoot(ElementName = "Element", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Element
{
[XmlElement(ElementName = "Requirement", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Requirement Requirement { get; set; }
[XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
}
[XmlRoot(ElementName = "AnyValueType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class AnyValueTypeElement : Element
{
}
[XmlRoot(ElementName = "Identifier")]
public class Identifier
{
[XmlAttribute(AttributeName = "schemeID")]
public string SchemeID { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Agent")]
public class Agent
{
[XmlElement(ElementName = "Identifier")]
public Identifier Identifier { get; set; }
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "ResponseOption", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public class ResponseOption
{
[XmlAttribute(AttributeName = "returnType")]
public string ReturnType { get; set; }
}
[XmlRoot(ElementName = "CurrentAddress")]
public class CurrentAddress
{
[XmlElement(ElementName = "FullAddress")]
public string FullAddress { get; set; }
[XmlElement(ElementName = "AdminUnitLevel1")]
public string AdminUnitLevel1 { get; set; }
}
[XmlRoot(ElementName = "Person")]
public class Person
{
[XmlElement(ElementName = "LevelOfAssurance")]
public string LevelOfAssurance { get; set; }
[XmlElement(ElementName = "Identifier")]
public Identifier Identifier { get; set; }
[XmlElement(ElementName = "FamilyName")]
public string FamilyName { get; set; }
[XmlElement(ElementName = "GivenName")]
public string GivenName { get; set; }
[XmlElement(ElementName = "DateOfBirth")]
public string DateOfBirth { get; set; }
[XmlElement(ElementName = "PlaceOfBirth")]
public string PlaceOfBirth { get; set; }
[XmlElement(ElementName = "CurrentAddress")]
public CurrentAddress CurrentAddress { get; set; }
[XmlElement(ElementName = "Gender")]
public string Gender { get; set; }
}
[XmlRoot(ElementName = "Title", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class Title
{
[XmlAttribute(AttributeName = "lang")]
public string Lang { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "DataServiceEvidenceType", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public class DataServiceEvidenceType
{
[XmlElement(ElementName = "Identifier", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public string Identifier2 { get; set; }
[XmlElement(ElementName = "EvidenceTypeClassification", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public string EvidenceTypeClassification { get; set; }
[XmlElement(ElementName = "Title", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public Title Title { get; set; }
}
[XmlRoot(ElementName = "Query", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public class Query
{
[XmlElement(ElementName = "Slot", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public List<Slot> Slot { get; set; }
[XmlAttribute(AttributeName = "queryDefinition")]
public string QueryDefinition { get; set; }
}
[XmlRoot(ElementName = "QueryRequest", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public class QueryRequest
{
[XmlElement(ElementName = "Slot", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:rim:4.0")]
public List<Slot> Slot { get; set; }
[XmlElement(ElementName = "ResponseOption", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public ResponseOption ResponseOption { get; set; }
[XmlElement(ElementName = "Query", Namespace = "urn:oasis:names:tc:ebxml-regrep:xsd:query:4.0")]
public Query Query { get; set; }
[XmlAttribute(AttributeName = "query", Namespace = "http://www.w3.org/2000/xmlns/")]
public string _Query { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
}
}https://stackoverflow.com/questions/73611926
复制相似问题