将复杂的xml反序列化为对象-创建对象的问题

本文关键字:创建对象 对象 问题 反序列化 复杂 xml | 更新日期: 2023-09-27 18:26:25

好的,所以我正在将xml文档反序列化为对象。基本上,我的xml文档将如下所示:

<?xml version="1.0"?>
<root>
    <eConnect CUSTNMBR="22222" DATE1="1900-01-01T00:00:00" TABLENAME="RM00101" DBNAME="BOLT" Requester_DOCTYPE="Customer" ACTION="0">
        <Customer>
            <CUSTNMBR>22222</CUSTNMBR>
            <ADDRESS1>123 ABC St</ADDRESS1>
            <ADDRESS2/>
            <ADDRESS3/>
            <ADRSCODE>PRIMARY</ADRSCODE>
            <CITY>Ann Arbor</CITY>
            <CNTCPRSN/>
            <COUNTRY>USA</COUNTRY>
            <CPRCSTNM/>
            <CURNCYID/>
            <CUSTCLAS/>
            <CUSTDISC>0</CUSTDISC>
            <CUSTNAME>Test Customer 2</CUSTNAME>
            <PHONE1>4165551234</PHONE1>
            <PHONE2/>
            <PHONE3/>
            <FAX/>
            <PYMTRMID/>
            <SALSTERR/>
            <SHIPMTHD/>
            <SLPRSNID/>
            <STATE>Micigan</STATE>
            <TAXSCHID/>
            <TXRGNNUM/>
            <UPSZONE/>
            <ZIP>45612</ZIP>
            <STMTNAME>Test Customer 2</STMTNAME>
            <SHRTNAME>Test Customer 2</SHRTNAME>
            <PRBTADCD>PRIMARY</PRBTADCD>
            <PRSTADCD>PRIMARY</PRSTADCD>
            <STADDRCD>PRIMARY</STADDRCD>
            <CHEKBKID/>
            <CRLMTTYP>0</CRLMTTYP>
            <CRLMTAMT>0.00000</CRLMTAMT>
            <CRLMTPER>0</CRLMTPER>
            <CRLMTPAM>0.00000</CRLMTPAM>
            <RATETPID/>
            <PRCLEVEL/>
            <MINPYTYP>0</MINPYTYP>
            <MINPYDLR>0.00000</MINPYDLR>
            <MINPYPCT>0</MINPYPCT>
            <FNCHATYP>0</FNCHATYP>
            <FNCHPCNT>0</FNCHPCNT>
            <FINCHDLR>0.00000</FINCHDLR>
            <MXWOFTYP>0</MXWOFTYP>
            <MXWROFAM>0.00000</MXWROFAM>
            <COMMENT1>test comment</COMMENT1>
            <COMMENT2>another test</COMMENT2>
            <USERDEF1/>
            <USERDEF2/>
            <TAXEXMT1/>
            <TAXEXMT2/>
            <BALNCTYP>0</BALNCTYP>
            <STMTCYCL>5</STMTCYCL>
            <BANKNAME/>
            <BNKBRNCH/>
            <FRSTINDT>1900-01-01T00:00:00</FRSTINDT>
            <INACTIVE>0</INACTIVE>
            <HOLD>0</HOLD>
            <CRCARDID/>
            <CRCRDNUM/>
            <CCRDXPDT>1900-01-01T00:00:00</CCRDXPDT>
            <KPDSTHST>1</KPDSTHST>
            <KPCALHST>1</KPCALHST>
            <KPERHIST>1</KPERHIST>
            <KPTRXHST>1</KPTRXHST>
            <CREATDDT>2015-11-27T00:00:00</CREATDDT>
            <MODIFDT>2015-11-27T00:00:00</MODIFDT>
            <Revalue_Customer>1</Revalue_Customer>
            <Post_Results_To>0</Post_Results_To>
            <FINCHID/>
            <GOVCRPID/>
            <GOVINDID/>
            <DISGRPER>0</DISGRPER>
            <DUEGRPER>0</DUEGRPER>
            <DOCFMTID/>
            <Send_Email_Statements>0</Send_Email_Statements>
            <GPSFOINTEGRATIONID/>
            <INTEGRATIONSOURCE>0</INTEGRATIONSOURCE>
            <INTEGRATIONID/>
            <Address>
                <CUSTNMBR>22222</CUSTNMBR>
                <ADRSCODE>PRIMARY</ADRSCODE>
                <SLPRSNID/>
                <UPSZONE/>
                <SHIPMTHD/>
                <TAXSCHID/>
                <CNTCPRSN/>
                <ADDRESS1>123 ABC St</ADDRESS1>
                <ADDRESS2/>
                <ADDRESS3/>
                <COUNTRY>USA</COUNTRY>
                <CITY>Ann Arbor</CITY>
                <STATE>Micigan</STATE>
                <ZIP>45612</ZIP>
                <PHONE1>4165551234</PHONE1>
                <PHONE2/>
                <PHONE3/>
                <FAX/>
                <GPSFOINTEGRATIONID/>
                <INTEGRATIONSOURCE>0</INTEGRATIONSOURCE>
                <INTEGRATIONID/>
                <Internet_Address/>
            </Address>
        </Customer>
    </eConnect>
</root>

当我尝试反序列化它时,我会得到一个"Object reference not set to instance of a Object"异常。我的课程是这样的:

    [XmlRoot("root")]
    public class eConnect
    {
        public Customer customer;
    }
    public class Customer
    {
        public string CUSTNMBR { get; set; }
        public string CUSTNAME { get; set; }
    }

我得到异常是因为我必须为xml文档中的所有节点都设置变量吗?有没有办法只使用我需要的节点?

此外,我创建类的方式是否正确,因为基本上xml将有一个名为root的根节点,并且该根节点可以包含多个eConnect节点,但每个eConnect节点将只包含一个客户节点。

将复杂的xml反序列化为对象-创建对象的问题

尝试在类中添加Serializable属性,并在类中使用XmlElement标记来定义对象的映射方式。我也会为你的根创建一个单独的类。

[XmlRoot("root")]
[Serializable]
public class Root
{
    public Root() 
    {
        eConnects = new List<eConnect>();
    }
    [XmlElement("eConnect")]
    public List<eConnect> eConnects { get; set; }
}
[XmlRoot("eConnect")]
[Serializable]
public class eConnect
{
    [XmlElement("Customer")]
    public Customer customer { get; set; }
}
[XmlRoot("Customer")]
[Serializable]
public class Customer
{
    [XmlElement("CUSTNMBR")]
    public string CUSTNMBR { get; set; }
    [XmlElement("CUSTNAME")]
    public string CUSTNAME { get; set; }
}

使用XmlElement标记还可以自由地为变量命名更友好的名称。例如,您可以切换

[XmlElement("CUSTNAME")]
public string CUSTNAME { get; set; }

[XmlElement("CUSTNAME")]
public string CustomerName { get; set; }

使用Visual Studio的将XML粘贴为类功能。

为此:Open Visual Studio > Edit Menu > Paste Special > Paste XML as Classes.

生成的类可能需要清理(例如,将类型从ushort更改为int等)。此功能处理序列化所需的属性。以下是您的xml示例:

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class root
{
    private rootEConnect eConnectField;
    /// <remarks/>
    public rootEConnect eConnect
    {
        get
        {
            return this.eConnectField;
        }
        set
        {
            this.eConnectField = value;
        }
    }
}
// Intermediate Xml nodes ...
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rootEConnectCustomer
{
    private ushort cUSTNMBRField;
    private string aDDRESS1Field;
    private object aDDRESS2Field;
    private object aDDRESS3Field;
    private string aDRSCODEField;
    // rest of class ...
}