C# 将XML字符串转换为对象

C# 将XML字符串转换为对象,c#,.net,xml,linq-to-xml,C#,.net,Xml,Linq To Xml,我有一个class订单 [Serializable()] [XmlRoot("Order")] public class Order { [XmlAttribute("Counter")] public int Counter { get; set; } [XmlAttribute("Conveyer")] public int Conveyer { get; set; } } 现在,我的XML字符串是: "<Order xmlns:xsi="http:/

我有一个class
订单

[Serializable()]
[XmlRoot("Order")]
public class Order
{
    [XmlAttribute("Counter")]
    public int Counter { get; set; }
    [XmlAttribute("Conveyer")]
    public int Conveyer { get; set; }
}
现在,我的XML字符串是:

"<Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Counter>3</Counter>
  <Conveyer>1</Conveyer>
</Order>"

您应该使用
XmlElementAttribute
而不是
XmlAttribute

[XmlElementAttribute("Counter")]
public int Counter { get; set; }
[XmlElementAttribute("Conveyer")]
public int Conveyer { get; set; }
这将有助于正确地反序列化


您应该使用
XmlElementAttribute
而不是
XmlAttribute

[XmlElementAttribute("Counter")]
public int Counter { get; set; }
[XmlElementAttribute("Conveyer")]
public int Conveyer { get; set; }
这将有助于正确地反序列化


我在我的项目中完成了这样的任务。像这样试试

[XmlElement(ElementName = "Counter")]
public int Counter { get; set; }

我在我的项目中做过这样的任务。像这样试试

[XmlElement(ElementName = "Counter")]
public int Counter { get; set; }