简体   繁体   English

将XML反序列化为对象-属性值归零

[英]Deserialize XML to Object - null value from attrubutes

When i try load xml configuration file to object, all xml elements was deserialized, but "xml attrubutes" load as null. 当我尝试将xml配置文件加载到对象时,所有xml元素都已反序列化,但是“ xml attrubutes”加载为null。

I have xml file: 我有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>

And i try to read it to object: 我尝试阅读它以反对:

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; }
    }

And it load CAS.rocord load 5 elements in array, but all atributes NR and NAZWA is null. 它加载CAS.rocord加载数组中的5个元素,但是所有属性NR和NAZWA为空。

The names in the [XmlAttribute(name)] attributes that appear in the Record class must correspond to the attribute names that appear in the XML. Record类中显示的[XmlAttribute(name)]属性中的名称必须与XML中出现的属性名称相对应。 You currently have NR and NAZWA , but these do not appear in the XML. 您目前有NRNAZWA ,但是它们没有出现在XML中。 Instead it includes the following attributes: 相反,它包含以下属性:

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

Thus your Record class needs to look like: 因此,您的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; }
}

Your classes should be like this 你的课应该是这样的

    [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; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM