C# 反序列化和XML的问题

C# 反序列化和XML的问题,c#,xml,visual-studio-2010,deserialization,xml-deserialization,C#,Xml,Visual Studio 2010,Deserialization,Xml Deserialization,我正在使用VisualStudio2010(c#),我的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%0

我正在使用VisualStudio2010(c#),我的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>
知道我遗漏了什么吗?
任何帮助都将不胜感激

CommandCollection类应该用属性
[System.Xml.Serialization.XmlRoot(“command_strings”)]标记

您还应该为
version
添加一个属性,并用
xmldattribute
属性标记它。

您能分享一下这是如何工作不正常的吗?由于某些原因,我无法从XML文件中获取XML属性值。是的,在传递时忽略了这一点。
[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();
}