2016-01-26 112 views
1

當我嘗試將xml配置文件加載到對象時,所有xml元素都被反序列化,但「xml attrubutes」加載爲null。將XML反序列化爲對象 - 來自attrubutes的空值

我有xml文件:

<?xml version="1.0" encoding="windows-1250"?> 
<CAS_POLSKA> 

<Komunikacja> 
    <Typ>RS232</Typ> 
    <Port>6</Port> 
    <BaudRate>115200</BaudRate> 
    <Interval>5000</Interval> 
    <Retry>3</Retry> 
    <TYPWAGI>POS</TYPWAGI> 
    <!-- model type of scale (CL5000, LP16, POSCALE, etc...). --> 
    <RODZAJWAGI>POSCALE</RODZAJWAGI> 
    <PASEKPOSTEPU>1</PASEKPOSTEPU> 
    <DEBUGMESS>1</DEBUGMESS> 
    <AKCJA>WYSYLKA</AKCJA> 
    <TYPDANYCH>PLU</TYPDANYCH> 
</Komunikacja> 

<DEFAULT DEPARTMENT="1" PLUTYPE="1" /> 

<Dane> 
    <!-- W polu Grupa definiujemy bitowo NW, FIX i BAR --> 

    <RECORD PLU="1" NAZWA01="AccesPoint LINKSYS WRT 54G" UNITPRICE="30378" ITEMCODE="1234"/> 
    <RECORD PLU="2" NAZWA01="Access Point EDIMAX EW-7209" UNITPRICE="30500" ITEMCODE="222" GROUP="1"/> 
    <RECORD PLU="3" NAZWA01="szynka" UNITPRICE="1000" ITEMCODE="3133" GROUP="3"/> 
    <RECORD PLU="4" NAZWA01="szynka" UNITPRICE="2000" ITEMCODE="3134" GROUP="4"/> 
    <RECORD PLU="5" NAZWA01="szynka" UNITPRICE="3000" GROUP="7"/> 
</Dane> 
</CAS_POLSKA> 

,我嘗試讀取它反對:

class Program 
    { 
     static void Main(string[] args) 
     { 
      System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(CAS_POLSKA)); 
      System.IO.StreamReader file = new System.IO.StreamReader(@"plu_cas.xml"); 
      CAS_POLSKA CAS = new CAS_POLSKA(); 
      CAS = (CAS_POLSKA)reader.Deserialize(file); 
     } 
    } 

    [Serializable()] 
    public class CAS_POLSKA 
    { 
     [System.Xml.Serialization.XmlElement("Komunikacja")] 
     public Komunikacja komunikacja { get; set; } 

     [System.Xml.Serialization.XmlArray("Dane")] 
     [System.Xml.Serialization.XmlArrayItem("RECORD", typeof(Record))] 
     public Record[] record { get; set; } 
    } 


    [Serializable()] 
    public class Komunikacja 
    { 
     //... 
     //this code load correctly 
    } 

    [Serializable()] 
    public class Record 
    { 
     [System.Xml.Serialization.XmlAttribute("NR")] 
     public int NR { get; set; } 
     [System.Xml.Serialization.XmlAttribute("NAZWA")] 
     public string NAZWA { get; set; } 
    } 

,並將其加載CAS.rocord負載陣列5個元素,但所有的屬性NR由...和一片空白。

回答

0

出現在Record類中的[XmlAttribute(name)]屬性中的名稱必須與XML中出現的屬性名稱相對應。您目前有NRNAZWA,但這些不會出現在XML中。相反,它包括以下屬性:

<RECORD 
    PLU="2" 
    NAZWA01="Access Point EDIMAX EW-7209" 
    UNITPRICE="30500" 
    ITEMCODE="222" 
    GROUP="1"/> 

因此您Record類需要的樣子:

public class Record 
{ 
    [System.Xml.Serialization.XmlAttribute("PLU")] 
    public int PLU { get; set; } 

    [System.Xml.Serialization.XmlAttribute("NAZWA01")] 
    public string Nazwa01 { get; set; } 

    [System.Xml.Serialization.XmlAttribute("UNITPRICE")] 
    public int UnitPrice { get; set; } 

    [System.Xml.Serialization.XmlAttribute("ITEMCODE")] 
    public string ItemCode { get; set; } 

    [System.Xml.Serialization.XmlAttribute("GROUP")] 
    public string Group { get; set; } 
} 
0

你的類應該是這樣的

[XmlRoot(ElementName="Komunikacja")] 
    public class Komunikacja { 
     [XmlElement(ElementName="Typ")] 
     public string Typ { get; set; } 
     [XmlElement(ElementName="Port")] 
     public string Port { get; set; } 
     [XmlElement(ElementName="BaudRate")] 
     public string BaudRate { get; set; } 
     [XmlElement(ElementName="Interval")] 
     public string Interval { get; set; } 
     [XmlElement(ElementName="Retry")] 
     public string Retry { get; set; } 
     [XmlElement(ElementName="TYPWAGI")] 
     public string TYPWAGI { get; set; } 
     [XmlElement(ElementName="RODZAJWAGI")] 
     public string RODZAJWAGI { get; set; } 
     [XmlElement(ElementName="PASEKPOSTEPU")] 
     public string PASEKPOSTEPU { get; set; } 
     [XmlElement(ElementName="DEBUGMESS")] 
     public string DEBUGMESS { get; set; } 
     [XmlElement(ElementName="AKCJA")] 
     public string AKCJA { get; set; } 
     [XmlElement(ElementName="TYPDANYCH")] 
     public string TYPDANYCH { get; set; } 
    } 

    [XmlRoot(ElementName="DEFAULT")] 
    public class DEFAULT { 
     [XmlAttribute(AttributeName="DEPARTMENT")] 
     public string DEPARTMENT { get; set; } 
     [XmlAttribute(AttributeName="PLUTYPE")] 
     public string PLUTYPE { get; set; } 
    } 

    [XmlRoot(ElementName="RECORD")] 
    public class RECORD { 
     [XmlAttribute(AttributeName="PLU")] 
     public string PLU { get; set; } 
     [XmlAttribute(AttributeName="NAZWA01")] 
     public string NAZWA01 { get; set; } 
     [XmlAttribute(AttributeName="UNITPRICE")] 
     public string UNITPRICE { get; set; } 
     [XmlAttribute(AttributeName="ITEMCODE")] 
     public string ITEMCODE { get; set; } 
     [XmlAttribute(AttributeName="GROUP")] 
     public string GROUP { get; set; } 
    } 

    [XmlRoot(ElementName="Dane")] 
    public class Dane { 
     [XmlElement(ElementName="RECORD")] 
     public List<RECORD> RECORD { get; set; } 
    } 

    [XmlRoot(ElementName="CAS_POLSKA")] 
    public class CAS_POLSKA { 
     [XmlElement(ElementName="Komunikacja")] 
     public Komunikacja Komunikacja { get; set; } 
     [XmlElement(ElementName="DEFAULT")] 
     public DEFAULT DEFAULT { get; set; } 
     [XmlElement(ElementName="Dane")] 
     public Dane Dane { get; set; } 
    } 
}