C# 如何序列化列表?
我是C# 如何序列化列表?,c#,xml,xml-serialization,C#,Xml,Xml Serialization,我是XmlSerializer的新手,我有一个列表,我想这样序列化: <?xml version="1.0" encoding="utf-16"?> <ArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <itens> <Item corret="0" ord
XmlSerializer
的新手,我有一个列表
,我想这样序列化:
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<itens>
<Item corret="0" order="�)">
foo
</Item>
<Item corret="1" order=")">
baa
</Item>
</itens>
</ArrayOfItem>
我正在使用以下命令序列化:
new XmlSerializer(typeof(List<Item>)).Serialize(Console.Out, list);
newxmlserializer(typeof(List)).Serialize(Console.Out,List);
您需要另一个属性类型为列表的类:
谢谢,它起作用了,但是关于删除测试
标记并将其内容移动到项目
?i、 e,生成'foo'而不是'foo',没有注意到它的不同。更新了我的答案。
[Serializable]
public class Item
{
[XmlIgnore]
public int correctIndex { get; set; }
[XmlIgnore]
public int index { get; set; }
[XmlAttribute("corret")]
public string IsCorrect
{
get { return correctIndex == index ? "1" : "0"; }
set { }
}
[XmlAttribute("order")]
public string Order
{
get { return string.Format("{0})", ((char)index).ToString()); }
set { }
}
[XmlElement("test")]
public string text { get; set; }
}
new XmlSerializer(typeof(List<Item>)).Serialize(Console.Out, list);
[Serializable]
public class Items
{
[XmlArray("itens")]
public List<Item> itens { get; set; }
}
var list = new Items() { itens = new List<Item>() { new Item() { correctIndex = 10, index = 11, text = "asdasd" } } };
new XmlSerializer(typeof(Items)).Serialize(Console.Out, list);
[XmlText]
public string text { get; set; }