1

好的,所以我正在将 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>

当我尝试反序列化它时,我得到一个“对象引用未设置为对象实例”异常。我的课程如下所示:

    [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 节点将只包含一个客户节点。

4

2 回答 2

2

尝试在您的类上添加 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; }
于 2015-12-11T21:51:07.653 回答
0

使用 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 ...
}
于 2019-10-14T16:11:13.260 回答