可能重複序列化清單:
How to rename <ArrayOf> XML attribute that generated after serializing List of objects一定的格式
這應該是很簡單的,但我在這裏失去了一些東西。序列化我的類時,我需要以下格式的輸出xml。
<items>
<item id="1" name="John">
<item id="2" name="Peter">
<item id="3" name="Shane">
<items>
這裏是類:
public class Item
{
[XmlAttribute]
public string Id;
[XmlAttribute]
public string Name;
}
這裏是我的測試代碼:
var items = new List<Item>();
for (int i = 0; i < 4; i++)
{
var item = new Item();
item.Id = i.ToString();
item.Name = "Jeff" + " - " + i.ToString();
items.Add(item);
}
Ser(items);
static void Ser(object o)
{
XmlSerializer x = new XmlSerializer(o.GetType());
var xns = new XmlSerializerNamespaces();
xns.Add(string.Empty, string.Empty);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
XmlWriter writer = XmlWriter.Create(Console.Out, settings);
x.Serialize(writer, o, xns);
Console.WriteLine();
}
輸出我得到的是:
<ArrayOfItem>
<Item Id="0" Name="Jeff - 0" />
<Item Id="1" Name="Jeff - 1" />
<Item Id="2" Name="Jeff - 2" />
<Item Id="3" Name="Jeff - 3" />
</ArrayOfItem>
我要替換「 ArrayOfItem「與」Items「。另外,爲什麼我會看到「ArrayOf」?提前致謝。
我想立即創建xml和這個工作KS很適合我。謝謝! – stech