XmlSerialization集合为阵 [英] XmlSerialization Collection as Array
本文介绍了XmlSerialization集合为阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想序列化需要使用同一名称的多个元素的自定义类。
我已经使用xmlarray尝试过,但它包装他们在另一个元素。
我想我的XML看起来像这样。
<根>
<树>一些文字< /棵>
<树>一些文字< /棵>
< /根>
我的code:
[序列化(),XmlRoot(根)
公共类测试
{
[XmlArray(树)]
公共ArrayList的MyProp1 =新的ArrayList();
公开测试()
{
MyProp1.Add(一些文本);
MyProp1.Add(一些文字);
}
}
解决方案
尽量只使用 [的XmlElement(树)]
:
[序列化(),XmlRoot(根)
公共类测试
{
[的XmlElement(树)]
公开名单<字符串> MyProp1 =新的名单,其中,串>();
公开测试()
{
MyProp1.Add(一些文本);
MyProp1.Add(一些文字);
}
}
请注意,我改变的ArrayList
到名单,其中,串>
清理输出;在1.1, StringCollection
将是另一种选择,尽管有不同的区分大小写的规则。
I'm trying to serialize a custom class that needs to use multiple elements of the same name.
I've tried using xmlarray, but it wraps them in another elements.
I want my xml to look like this.
<root>
<trees>some text</trees>
<trees>some more text</trees>
</root>
My code:
[Serializable(), XmlRoot("root")]
public class test
{
[XmlArray("trees")]
public ArrayList MyProp1 = new ArrayList();
public test()
{
MyProp1.Add("some text");
MyProp1.Add("some more text");
}
}
解决方案
Try just using [XmlElement("trees")]
:
[Serializable(), XmlRoot("root")]
public class test
{
[XmlElement("trees")]
public List<string> MyProp1 = new List<string>();
public test()
{
MyProp1.Add("some text");
MyProp1.Add("some more text");
}
}
Note I changed ArrayList
to List<string>
to clean up the output; in 1.1, StringCollection
would be another option, although that has different case-sensitivity rules.
这篇关于XmlSerialization集合为阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文