如何将此XML文档反序列化为生成的C#类

本文关键字:反序列化 XML 文档 | 更新日期: 2023-09-27 18:24:22

我尝试过阅读XML文档,发现了一种将XSD文件生成为C#类的方法。

XML文档:

    <?xml version="1.0" standalone="yes"?>
<sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/sdnList.xsd">
  <publshInformation>
    <Publish_Date>12/15/2011</Publish_Date>
    <Record_Count>5052</Record_Count>
  </publshInformation>
  <sdnEntry>
    <uid>11691</uid>
    <lastName>SERVICIOS DE CONTROL INTEGRAL DE OBRAS S.L. SIP SUCURSAL CARTAGENA</lastName>
    <sdnType>Entity</sdnType>
    <programList>
      <program>SDNT</program>
    </programList>
    <idList>
      <id>
        <uid>6028</uid>
        <idType>NIT #</idType>
        <idNumber>900106267-0</idNumber>
        <idCountry>Colombia</idCountry>
      </id>
    </idList>
    <addressList>
      <address>
        <uid>17543</uid>
        <address1>Carrera 3 No. 8-38</address1>
        <city>Cartagena</city>
        <country>Colombia</country>
      </address>
      <address>
        <uid>17544</uid>
        <address1>Carrera 4 No. 8-41</address1>
        <city>Cartagena</city>
        <country>Colombia</country>
      </address>
    </addressList>
  </sdnEntry>
  <sdnEntry>
    <uid>11692</uid>
    <lastName>INVERSIONES EL PROGRESO S.A.</lastName>
    <sdnType>Entity</sdnType>
    <programList>
      <program>SDNT</program>
    </programList>
    <idList>
      <id>
        <uid>6027</uid>
        <idType>NIT #</idType>
        <idNumber>806006517-7</idNumber>
        <idCountry>Colombia</idCountry>
      </id>
    </idList>
    <akaList>
      <aka>
        <uid>12373</uid>
        <type>a.k.a.</type>
        <category>weak</category>
        <lastName>I.P. S.A.</lastName>
      </aka>
    </akaList>
    <addressList>
      <address>
        <uid>17540</uid>
        <address1>Carrera 3 No. 8-38 Ofc. 1</address1>
        <city>Cartagena</city>
        <country>Colombia</country>
      </address>
      <address>
        <uid>17541</uid>
        <address1>Carrera 4 No. 8-41</address1>
        <city>Cartagena</city>
        <country>Colombia</country>
      </address>
      <address>
        <uid>17542</uid>
        <address1>Olaya Herrera Carrera 68 No. 32B-45</address1>
        <city>Cartagena</city>
        <country>Colombia</country>
      </address>
    </addressList>
  </sdnEntry>

XSD tool 生成C#类

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.5448
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
// 
// This source code was auto-generated by xsd, Version=2.0.50727.42.
// 

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/sdnList.xsd", IsNullable = false)]
public partial class sdnList
{
    private sdnListPublshInformation publshInformationField;
    private sdnListSdnEntry[] sdnEntryField;
    /// <remarks/>
    public sdnListPublshInformation publshInformation
    {
        get
        {
            return this.publshInformationField;
        }
        set
        {
            this.publshInformationField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("sdnEntry")]
    public sdnListSdnEntry[] sdnEntry
    {
        get
        {
            return this.sdnEntryField;
        }
        set
        {
            this.sdnEntryField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListPublshInformation
{
    private string publish_DateField;
    private int record_CountField;
    private bool record_CountFieldSpecified;
    /// <remarks/>
    public string Publish_Date
    {
        get
        {
            return this.publish_DateField;
        }
        set
        {
            this.publish_DateField = value;
        }
    }
    /// <remarks/>
    public int Record_Count
    {
        get
        {
            return this.record_CountField;
        }
        set
        {
            this.record_CountField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool Record_CountSpecified
    {
        get
        {
            return this.record_CountFieldSpecified;
        }
        set
        {
            this.record_CountFieldSpecified = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntry
{
    private int uidField;
    private string firstNameField;
    private string lastNameField;
    private string titleField;
    private string sdnTypeField;
    private string remarksField;
    private string[] programListField;
    private sdnListSdnEntryID[] idListField;
    private sdnListSdnEntryAka[] akaListField;
    private sdnListSdnEntryAddress[] addressListField;
    private sdnListSdnEntryNationality[] nationalityListField;
    private sdnListSdnEntryCitizenship[] citizenshipListField;
    private sdnListSdnEntryDateOfBirthItem[] dateOfBirthListField;
    private sdnListSdnEntryPlaceOfBirthItem[] placeOfBirthListField;
    private sdnListSdnEntryVesselInfo vesselInfoField;
    /// <remarks/>
    public int uid
    {
        get
        {
            return this.uidField;
        }
        set
        {
            this.uidField = value;
        }
    }
    /// <remarks/>
    public string firstName
    {
        get
        {
            return this.firstNameField;
        }
        set
        {
            this.firstNameField = value;
        }
    }
    /// <remarks/>
    public string lastName
    {
        get
        {
            return this.lastNameField;
        }
        set
        {
            this.lastNameField = value;
        }
    }
    /// <remarks/>
    public string title
    {
        get
        {
            return this.titleField;
        }
        set
        {
            this.titleField = value;
        }
    }
    /// <remarks/>
    public string sdnType
    {
        get
        {
            return this.sdnTypeField;
        }
        set
        {
            this.sdnTypeField = value;
        }
    }
    /// <remarks/>
    public string remarks
    {
        get
        {
            return this.remarksField;
        }
        set
        {
            this.remarksField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("program", IsNullable = false)]
    public string[] programList
    {
        get
        {
            return this.programListField;
        }
        set
        {
            this.programListField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("id", IsNullable = false)]
    public sdnListSdnEntryID[] idList
    {
        get
        {
            return this.idListField;
        }
        set
        {
            this.idListField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("aka", IsNullable = false)]
    public sdnListSdnEntryAka[] akaList
    {
        get
        {
            return this.akaListField;
        }
        set
        {
            this.akaListField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("address", IsNullable = false)]
    public sdnListSdnEntryAddress[] addressList
    {
        get
        {
            return this.addressListField;
        }
        set
        {
            this.addressListField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("nationality", IsNullable = false)]
    public sdnListSdnEntryNationality[] nationalityList
    {
        get
        {
            return this.nationalityListField;
        }
        set
        {
            this.nationalityListField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("citizenship", IsNullable = false)]
    public sdnListSdnEntryCitizenship[] citizenshipList
    {
        get
        {
            return this.citizenshipListField;
        }
        set
        {
            this.citizenshipListField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("dateOfBirthItem", IsNullable = false)]
    public sdnListSdnEntryDateOfBirthItem[] dateOfBirthList
    {
        get
        {
            return this.dateOfBirthListField;
        }
        set
        {
            this.dateOfBirthListField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("placeOfBirthItem", IsNullable = false)]
    public sdnListSdnEntryPlaceOfBirthItem[] placeOfBirthList
    {
        get
        {
            return this.placeOfBirthListField;
        }
        set
        {
            this.placeOfBirthListField = value;
        }
    }
    /// <remarks/>
    public sdnListSdnEntryVesselInfo vesselInfo
    {
        get
        {
            return this.vesselInfoField;
        }
        set
        {
            this.vesselInfoField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryID
{
    private int uidField;
    private string idTypeField;
    private string idNidCountryumberField;
    private string idCountryField;
    private string issueDateField;
    private string expirationDateField;
    /// <remarks/>
    public int uid
    {
        get
        {
            return this.uidField;
        }
        set
        {
            this.uidField = value;
        }
    }
    /// <remarks/>
    public string idType
    {
        get
        {
            return this.idTypeField;
        }
        set
        {
            this.idTypeField = value;
        }
    }
    /// <remarks/>
    public string idNidCountryumber
    {
        get
        {
            return this.idNidCountryumberField;
        }
        set
        {
            this.idNidCountryumberField = value;
        }
    }
    /// <remarks/>
    public string idCountry
    {
        get
        {
            return this.idCountryField;
        }
        set
        {
            this.idCountryField = value;
        }
    }
    /// <remarks/>
    public string issueDate
    {
        get
        {
            return this.issueDateField;
        }
        set
        {
            this.issueDateField = value;
        }
    }
    /// <remarks/>
    public string expirationDate
    {
        get
        {
            return this.expirationDateField;
        }
        set
        {
            this.expirationDateField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryAka
{
    private int uidField;
    private string typeField;
    private string categoryField;
    private string lastNameField;
    private string firstNameField;
    /// <remarks/>
    public int uid
    {
        get
        {
            return this.uidField;
        }
        set
        {
            this.uidField = value;
        }
    }
    /// <remarks/>
    public string type
    {
        get
        {
            return this.typeField;
        }
        set
        {
            this.typeField = value;
        }
    }
    /// <remarks/>
    public string category
    {
        get
        {
            return this.categoryField;
        }
        set
        {
            this.categoryField = value;
        }
    }
    /// <remarks/>
    public string lastName
    {
        get
        {
            return this.lastNameField;
        }
        set
        {
            this.lastNameField = value;
        }
    }
    /// <remarks/>
    public string firstName
    {
        get
        {
            return this.firstNameField;
        }
        set
        {
            this.firstNameField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryAddress
{
    private int uidField;
    private string address1Field;
    private string address2Field;
    private string address3Field;
    private string cityField;
    private string stateOrProvinceField;
    private string postalCodeField;
    private string countryField;
    /// <remarks/>
    public int uid
    {
        get
        {
            return this.uidField;
        }
        set
        {
            this.uidField = value;
        }
    }
    /// <remarks/>
    public string address1
    {
        get
        {
            return this.address1Field;
        }
        set
        {
            this.address1Field = value;
        }
    }
    /// <remarks/>
    public string address2
    {
        get
        {
            return this.address2Field;
        }
        set
        {
            this.address2Field = value;
        }
    }
    /// <remarks/>
    public string address3
    {
        get
        {
            return this.address3Field;
        }
        set
        {
            this.address3Field = value;
        }
    }
    /// <remarks/>
    public string city
    {
        get
        {
            return this.cityField;
        }
        set
        {
            this.cityField = value;
        }
    }
    /// <remarks/>
    public string stateOrProvince
    {
        get
        {
            return this.stateOrProvinceField;
        }
        set
        {
            this.stateOrProvinceField = value;
        }
    }
    /// <remarks/>
    public string postalCode
    {
        get
        {
            return this.postalCodeField;
        }
        set
        {
            this.postalCodeField = value;
        }
    }
    /// <remarks/>
    public string country
    {
        get
        {
            return this.countryField;
        }
        set
        {
            this.countryField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryNationality
{
    private int uidField;
    private string countryField;
    private bool mainEntryField;
    /// <remarks/>
    public int uid
    {
        get
        {
            return this.uidField;
        }
        set
        {
            this.uidField = value;
        }
    }
    /// <remarks/>
    public string country
    {
        get
        {
            return this.countryField;
        }
        set
        {
            this.countryField = value;
        }
    }
    /// <remarks/>
    public bool mainEntry
    {
        get
        {
            return this.mainEntryField;
        }
        set
        {
            this.mainEntryField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryCitizenship
{
    private int uidField;
    private string countryField;
    private bool mainEntryField;
    /// <remarks/>
    public int uid
    {
        get
        {
            return this.uidField;
        }
        set
        {
            this.uidField = value;
        }
    }
    /// <remarks/>
    public string country
    {
        get
        {
            return this.countryField;
        }
        set
        {
            this.countryField = value;
        }
    }
    /// <remarks/>
    public bool mainEntry
    {
        get
        {
            return this.mainEntryField;
        }
        set
        {
            this.mainEntryField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryDateOfBirthItem
{
    private int uidField;
    private string dateOfBirthField;
    private bool mainEntryField;
    /// <remarks/>
    public int uid
    {
        get
        {
            return this.uidField;
        }
        set
        {
            this.uidField = value;
        }
    }
    /// <remarks/>
    public string dateOfBirth
    {
        get
        {
            return this.dateOfBirthField;
        }
        set
        {
            this.dateOfBirthField = value;
        }
    }
    /// <remarks/>
    public bool mainEntry
    {
        get
        {
            return this.mainEntryField;
        }
        set
        {
            this.mainEntryField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryPlaceOfBirthItem
{
    private int uidField;
    private string placeOfBirthField;
    private bool mainEntryField;
    /// <remarks/>
    public int uid
    {
        get
        {
            return this.uidField;
        }
        set
        {
            this.uidField = value;
        }
    }
    /// <remarks/>
    public string placeOfBirth
    {
        get
        {
            return this.placeOfBirthField;
        }
        set
        {
            this.placeOfBirthField = value;
        }
    }
    /// <remarks/>
    public bool mainEntry
    {
        get
        {
            return this.mainEntryField;
        }
        set
        {
            this.mainEntryField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/sdnList.xsd")]
public partial class sdnListSdnEntryVesselInfo
{
    private string callSignField;
    private string vesselTypeField;
    private string vesselFlagField;
    private string vesselOwnerField;
    private int tonnageField;
    private bool tonnageFieldSpecified;
    private int grossRegisteredTonnageField;
    private bool grossRegisteredTonnageFieldSpecified;
    /// <remarks/>
    public string callSign
    {
        get
        {
            return this.callSignField;
        }
        set
        {
            this.callSignField = value;
        }
    }
    /// <remarks/>
    public string vesselType
    {
        get
        {
            return this.vesselTypeField;
        }
        set
        {
            this.vesselTypeField = value;
        }
    }
    /// <remarks/>
    public string vesselFlag
    {
        get
        {
            return this.vesselFlagField;
        }
        set
        {
            this.vesselFlagField = value;
        }
    }
    /// <remarks/>
    public string vesselOwner
    {
        get
        {
            return this.vesselOwnerField;
        }
        set
        {
            this.vesselOwnerField = value;
        }
    }
    /// <remarks/>
    public int tonnage
    {
        get
        {
            return this.tonnageField;
        }
        set
        {
            this.tonnageField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool tonnageSpecified
    {
        get
        {
            return this.tonnageFieldSpecified;
        }
        set
        {
            this.tonnageFieldSpecified = value;
        }
    }
    /// <remarks/>
    public int grossRegisteredTonnage
    {
        get
        {
            return this.grossRegisteredTonnageField;
        }
        set
        {
            this.grossRegisteredTonnageField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool grossRegisteredTonnageSpecified
    {
        get
        {
            return this.grossRegisteredTonnageFieldSpecified;
        }
        set
        {
            this.grossRegisteredTonnageFieldSpecified = value;
        }
    }
}

我的代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace Validus.New.Domain
{
    class Testdeserialize
    {
        public void test()
        {
            sdnList sdnEntry = null;
            string path = "D:/sdn1.xml";
            XmlSerializer serializer = new XmlSerializer(typeof(sdnList));

            StreamReader reader = new StreamReader(path);
            sdnEntry = (sdnList)serializer.Deserialize(reader);
            reader.Close();
        }
    }
}

但它不起作用。

那么如何将这个XML文档反序列化为C#类对象呢

如何将此XML文档反序列化为生成的C#类

我认为您对序列化程序/反序列化程序没有任何问题,相反,您在这里遇到了问题:

StreamReader reader = new StreamReader(path); 
sdnEntry = (sdnList)serializer.Deserialize(reader); 
reader.Close(); 

尝试:

using(var fs = File.OpenRead(path))
{
    sdnEntry = (sdnList)serializer.Deserialize(reader); 
}
相关文章: