好的,所以我正在将 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 节点将只包含一个客户节点。