当我尝试将xml配置文件加载到object时,所有xml元素都被反序列化,但“xml attrubutes”加载为null。
我有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和NAZWA都为空。
答案 0 :(得分:0)
Record
类中出现的[XmlAttribute(name)]
属性中的名称必须与XML中显示的属性名称相对应。您目前有NR
和NAZWA
,但这些不会出现在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; }
}
答案 1 :(得分: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; }
}
}