更改的XmlElement名称XML序列化 [英] Change XmlElement name for XML serialisation
本文介绍了更改的XmlElement名称XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们有followig code:
We have the followig code:
[Serializable]
public class Class1
{
[XmlElement("description")]
public string Description { get; set; }
}
class Program
{
static void Main(string[] args)
{
var list = new List<Class1> {new Class1() {Description = "Desc1"}, new Class1() {Description = "Desc2"}};
var serializer = new XmlSerializer(typeof(List<Class1>), new XmlRootAttribute("root"));
var ms = new MemoryStream();
serializer.Serialize(ms, list);
ms.Position = 0;
var result = new StreamReader(ms).ReadToEnd();
}
}
执行后,我们将在'结果'变量如下:
after execution we will have the following in 'result' variable:
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Class1>
<description>Desc1</description>
</Class1>
<Class1>
<description>Desc2</description>
</Class1>
</root>
现在的问题是:如何从1级更改XML元素的名称为项目1不改变类的名称
The question is: how to change xml elements name from 'Class1' to 'Item1' without changing class name?
推荐答案
您可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmltypeattribute.typename.aspx"><$c$c>XmlTypeAttribute.TypeName$c$c>对于这一点。
You can use XmlTypeAttribute.TypeName
for this.
试试这个为你 1级
定义
[XmlType(TypeName = "Item1")]
[Serializable]
public class Class1
{
[XmlElement("description")]
public string Description { get; set; }
}
这篇关于更改的XmlElement名称XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文