1

我正在尝试反序列化来自 api 的 xml。我已经使用 xsd.exe 将 xsd 转换为 c# 类。我正在使用的课程如下:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute("OfficerSearch", Namespace="http://xmlgw.companieshouse.gov.uk/v1-0/schema", IsNullable=false)]
public partial class OfficerSearch {

    private string continuationKeyField;

    private string searchRowsField;

    private OfficerSearchItem[] officerSearchItemField;

    /// <remarks/>
    public string ContinuationKey {
        get {
            return this.continuationKeyField;
        }
        set {
            this.continuationKeyField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
    public string SearchRows {
        get {
            return this.searchRowsField;
        }
        set {
            this.searchRowsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("OfficerSearchItem")]
    public OfficerSearchItem[] OfficerSearchItem {
        get {
            return this.officerSearchItemField;
        }
        set {
            this.officerSearchItemField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute("OfficerSearchItem", Namespace="", IsNullable=false)]
public partial class OfficerSearchItem : OfficerSearchItemBase {

    private string personIDField;

    private SearchMatch searchMatchField;

    private bool searchMatchFieldSpecified;

    private OfficerSearchItemBase[] duplicateOfficersField;

    /// <remarks/>
    public string PersonID {
        get {
            return this.personIDField;
        }
        set {
            this.personIDField = value;
        }
    }

    /// <remarks/>
    public SearchMatch SearchMatch {
        get {
            return this.searchMatchField;
        }
        set {
            this.searchMatchField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool SearchMatchSpecified {
        get {
            return this.searchMatchFieldSpecified;
        }
        set {
            this.searchMatchFieldSpecified = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("DuplicateOfficerSearchItem", IsNullable=false)]
    public OfficerSearchItemBase[] DuplicateOfficers {
        get {
            return this.duplicateOfficersField;
        }
        set {
            this.duplicateOfficersField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("DuplicateOfficerSearchItem", Namespace="", IsNullable=false)]
public partial class OfficerSearchItemBase {

    private string titleField;

    private string surnameField;

    private string forenameField;

    private System.DateTime dOBField;

    private string postTownField;

    private string postcodeField;

    private string countryOfResidenceField;

    /// <remarks/>
    public string Title {
        get {
            return this.titleField;
        }
        set {
            this.titleField = value;
        }
    }

    /// <remarks/>
    public string Surname {
        get {
            return this.surnameField;
        }
        set {
            this.surnameField = value;
        }
    }

    /// <remarks/>
    public string Forename {
        get {
            return this.forenameField;
        }
        set {
            this.forenameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType="date")]
    public System.DateTime DOB {
        get {
            return this.dOBField;
        }
        set {
            this.dOBField = value;
        }
    }

    /// <remarks/>
    public string PostTown {
        get {
            return this.postTownField;
        }
        set {
            this.postTownField = value;
        }
    }

    /// <remarks/>
    public string Postcode {
        get {
            return this.postcodeField;
        }
        set {
            this.postcodeField = value;
        }
    }

    /// <remarks/>
    public string CountryOfResidence {
        get {
            return this.countryOfResidenceField;
        }
        set {
            this.countryOfResidenceField = value;
        }
    }
}

除OfficerSearchItems 数组外,所有内容都反序列化。这些类对应的 xml 架构是:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Companies House XML Gateway Schema -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:include schemaLocation="chbase-v2-3.xsd"/>
    <xs:element name="OfficerSearchRequest">
        <xs:annotation>
            <xs:documentation>Officer Search Request</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Surname"/>
                <xs:element ref="Forename" minOccurs="0" maxOccurs="2"/>
                <xs:element ref="PostTown" minOccurs="0"/>
                <xs:element ref="OfficerType"/>
                <xs:element name="CountryOfResidence" type="xs:string" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Normal country of residence for the officer</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element ref="IncludeResignedInd" minOccurs="0"/>
                <xs:element ref="ContinuationKey" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="OfficerSearch">
        <xs:annotation>
            <xs:documentation>Officer Search Result</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="ContinuationKey" minOccurs="0"/>
                <xs:element ref="SearchRows"/>
                <xs:element ref="OfficerSearchItem" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

一些示例输出是:

    <OfficerSearch xmlns="http://xmlgw.companieshouse.gov.uk/v1-0/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema http://xmlgw.companieshouse.gov.uk/v1-0/schema/OfficerSearch-v2-3.xsd">
  <ContinuationKey>f80bf6eJyl0ttr1EAUB+C1bbrb6YqXqvF+rdqigSRLV8zbjoKttiqtYH0aJpmTJuhmYibB7qP+5f5mrLIUHwQDH8wl58yZk3j9Ne5xNhSZzApS7+URGT74ztlAyLpuZzVx1rP7RXlUkGntC25/VdQYvu2mKTVugdkA08q2+5VgWaWfaWb4Qq/XO8OG4dxj81V07JK9oRn3sRtjfRSGURwjVGQVEtvCBu4Y+xI7fKGntaxKMtu6M5Qk7/K8zKg5INlkRZLo+em2rNQXTKZlW4jJ/v7k00Z4nMaxTNNsU0TB38rZ1VK5k04Va8uRU7Ll9EVuhzH3l17rouLME11XKs7Wt7I0zEcUBXGksoDGRME4zWWg4mdjGW1R/FyNOFsRtTZtq79Vc9ki7vcnVVvoamYvbLrGHcc8V71dUqX5ulOpk8Ybd8MP9tt4NmWuG3IRttULf3L9LnEoTjrjIvzFlzsHP5LTvTzc23318Z9a+h+hm/ZXsDXCIiyBB8vQhwGsAINVGMJZOAfn4QJchDW4BJfhCvhwFa7BdbgBN+EW3IY7cBfuwX14AOvwEB7BY9iAJ/D0J6I2wvE=</ContinuationKey>
  <SearchRows>35</SearchRows>
  <OfficerSearchItem>
    <Title></Title>
    <Surname>SLATER</Surname>
    <Forename>DAVID STUART</Forename>
    <DOB>1966-09-15</DOB>
    <PostTown>MUCH WENLOCK</PostTown>
    <Postcode>TF13 </Postcode>
    <PersonID>CSeJxNkNFqgzAUhu/3FOK91aTVbZCmCG7gGAxa21uJ5lhDa3RJHPPt1+isvQn/+b6fcBKy+22uzg8oLVq5ddEqcB2QZcuFPG/dY/buvbjOjj6RCwy3k3WdNsz0miLiL4PlZuiAjnBKXKhKsgYQjaWpWzkQf0F3i+lHW8tF4Um1CuxEk/iUJs4hO8b7bCrNxtZ0r8Z8+Iyzt/3oZ2S1EeZqV1oiF/o7ldwuP0dyySEfcpFz+lVVogSVJsRfIOnYGezjw7IIqjUgDyNeehABeFFRMY/j54ihEPArXxN/bpMOlOybguIgCDYojBC+yX9GNDBV1tn0Y4+Dvlf8h2gv/AOdGJNe</PersonID>
    </OfficerSearchItem>
  <OfficerSearchItem>
    <Title></Title>
    <Surname>SLATER</Surname>
    <Forename>PETER THOMAS ANTHONY</Forename>
    <DOB>1960-01-29</DOB>
    <PostTown>PRESTON</PostTown>
    <Postcode>PR2 </Postcode>
    <PersonID>CSeJxNkF1PgzAUhu/9FYR7hMKGLum6kDjjjG7G4YVXpNDT0WwUbIuRfy+FMHbTPO9HetqDN3/VxfkFpUUt1y66D1wHZFEzIU9r9yt99h5dZ0Pu8Bm6/qRNow01rSYI+7OwvukaIIM5EhOKS1oBIok0ZS077M/WNQ3Ja13KOQrHqFZgFfnYpttPJ305vCdHJ9n3sP8ey1PD1nWrBj6+JX19yCfLxkaYi33ajEzon51k9hMT4nMGWZeJjJED56IAtXvC/mzihp7ALmFZ5AGPAHkhYoUHMYAX55x6LHyIKVpCuGIR9qc2bkDJtspJEKAVCqLFop85eVgDVUWZjpu7Ffpa8W/QXvgPvUeVog==</PersonID>
    </OfficerSearchItem>
</OfficerSearch>

任何人都可以阐明正在发生的事情,并给我一些指示/解决方案吗?干杯肖恩。

4

0 回答 0