0
我正在使用Visual Studio 2010(c#),我的XML反序列化代碼存在一些問題。我無法正確讀取我的XML。反序列化和XML的問題
我的XML內容如下:
<?xml version="1.0" encoding="utf-8"?>
<command_strings version="1">
<commands>
<command cmd_id="1" state_id="1" label="On" cmd_type="fixed" cmd_string="1%0D" />
<command cmd_id="1" state_id="3" label="Off" cmd_type="fixed" cmd_string="0%0d" />
</commands>
</command_strings>
我的代碼如下所示:
[Serializable()]
public class Command
{
[System.Xml.Serialization.XmlAttribute("cmd_id")]
public int cmd_id { get; set; }
[System.Xml.Serialization.XmlAttribute("state_id")]
public int state_id { get; set; }
[System.Xml.Serialization.XmlAttribute("label")]
public string label { get; set; }
[System.Xml.Serialization.XmlAttribute("cmd_type")]
public string cmd_type { get; set; }
[System.Xml.Serialization.XmlAttribute("cmd_string")]
public string cmd_string { get; set; }
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("commands")]
public class CommandCollection
{
[XmlArray("commands")]
[XmlArrayItem("command", typeof(Command))]
public Command[] Command { get; set; }
}
public void XMLStrings(string myXML)
{
CommandCollection commandscollection = null;
XmlSerializer dserial = new XmlSerializer(typeof(CommandCollection));
StreamReader streamReader = new StreamReader(@"C:\123.xml");
commandscollection = (CommandCollection)dserial.Deserialize(streamReader);
streamReader.Close();
}
任何想法,我可能會丟失? 任何幫助將不勝感激!
你能分享這是不是正常工作? – psubsee2003 2012-08-06 04:16:46
由於某些原因,我無法從XML文件中獲取XML屬性值。 – Miguel 2012-08-06 04:26:22
是@@ C:123.xml「'錯字嗎? – 2012-08-06 06:27:51