序列化数组没有根元素 [英] Serialize Array without root element
本文介绍了序列化数组没有根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图得到这个结果,而XML序列化
I'm trying to get to this result while serializing XML
<Test>
<Category>
<FileName>C:\test.txt</FileName>
<!-- Note that here this is an array of a simple class with two fields
without root -->
<Prop1>1</Prop1>
<Prop2>2</Prop2>
<Prop1>4</Prop1>
<Prop2>5</Prop2>
<!-- End array -->
</Category>
</Test>
我已经尝试不同的东西像这样
I already try different things like this
[Serializable]
[XmlRoot("Test")]
public class Test
{
[XmlElement("Category")]
public List<Category> Category= new List<Category>();
}
[Serializable]
[XmlRoot("Category")]
public class Category
{
[XmlElement("FileName")]
public string FileName { get; set; }
[XmlElement("Property")]
public List<Property> Properties = new List<Property>();
}
[Serializable]
public class Property
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
但我仍然得到这样的输出:
But I still get this output:
<Test>
<Category>
<FileName>C:\test.txt</FileName>
<Property>
<Prop1>1</Prop1>
<Prop2>2</Prop2>
</Property>
<Property>
<Prop1>4</Prop1>
<Prop2>5</Prop2>
</Property>
</Category>
</Test>
我怎样才能删除属性标记?
感谢很多提前
How can I remove the Property tag ?? Thanks a lot in advance
推荐答案
在情况下,如果你真的需要精确的输出,如上述规定,你可以使用的解决方法是这样的:
In case if you really need the exact output, as specified above, you can use workaround like this:
[Serializable]
public partial class Test {
public List<Category> Category;
}
[Serializable]
public partial class Category {
[XmlElement("FileName")]
public string FileName;
[XmlElement("Prop1")]
[XmlElement("Prop2")]
[XmlChoiceIdentifier("propName")]
public string[] Properties;
[XmlIgnore]
public PropName[] propName;
}
public enum PropName {
Prop1,
Prop2,
}
这篇关于序列化数组没有根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文