使用 c# 属性将友好名称属性添加到 xml 元素以进行序列化?
Add friendly name attribute to xml element using c# attribute for serialization?
我有一些从数据库生成的实体,我想将它们序列化为 XML。问题是一些实体的列名称对用户来说不是那么友好,这反映在生成的实体的字段中。
是否有可以应用于 class 字段的 C# 属性,以便我可以将包含友好名称值的 xml 属性添加到 xml元素本身?例如,使用类似于 XmlAttribute:
的东西
public class Book
{
public string Title { get; set; }
[XmlAttribute("friendlyName","International Standard Book Number")]
public string ISBN { get; set; }
}
序列化时应该产生类似这样的结果:
<Book>
<Title>To Kill a Mockingbird</Title>
<ISBN friendlyName="International Standard Book Number">9780061120084</ISBN>
</Book>
没有一个属性可以做到这一点。将元素文档与 xml 数据混合使用并不常见。通常,您希望使用 xsd 文档或其他方式记录您的 xml 模式。
话虽这么说,但这是您可以做到的。您需要将 ISBN
属性 从字符串更改为具有可以序列化的 friendlyName
属性 的自定义类型。
public class Book
{
public string Title { get; set; }
public ISBN ISBN { get; set; }
}
public class ISBN
{
[XmlText]
public string value { get; set; }
[XmlAttribute]
public string friendlyName { get; set; }
}
以下内容将完全按照您的问题进行序列化。
Book b = new Book
{
Title = "To Kill a Mockingbird",
ISBN = new ISBN
{
value = "9780061120084",
friendlyName = "International Standard Book Number",
}
};
更新
好的,另一种方法是创建一个自定义 XmlWriter
,它可以拦截序列化程序为创建元素所做的调用。当为要添加友好名称的 属性 创建元素时,您可以在自己的自定义属性中写入。
public class MyXmlTextWriter : XmlTextWriter
{
public MyXmlTextWriter(TextWriter w)
: base(w)
{
}
public override void WriteStartElement(string prefix, string localName, string ns)
{
base.WriteStartElement(prefix, localName, ns);
switch(localName)
{
case "ISBN":
WriteAttributeString("friendlyName", "International Standard Book Number");
break;
}
}
}
这是一个如何使用它的示例(来自控制台应用程序):
XmlSerializer serializer = new XmlSerializer(typeof(Book));
serializer.Serialize(new MyXmlTextWriter(Console.Out), b);
如果您需要能够写入其他内容,例如 Stream
。
,您可以实现 XmlTextWriter
的其他构造函数
试试这个
[XmlRoot("Book")]
public class Book
{
[XmlElement("Title")]
public string Title { get; set; }
[XmlElement("ISBN")]
public ISBN isbn { get; set; }
}
[XmlRoot("ISBN")]
public class ISBN
{
[XmlAttribute("friendlyName")]
public string friendlyName { get; set; }
[XmlText]
public string value { get; set; }
}
我有一些从数据库生成的实体,我想将它们序列化为 XML。问题是一些实体的列名称对用户来说不是那么友好,这反映在生成的实体的字段中。
是否有可以应用于 class 字段的 C# 属性,以便我可以将包含友好名称值的 xml 属性添加到 xml元素本身?例如,使用类似于 XmlAttribute:
的东西public class Book
{
public string Title { get; set; }
[XmlAttribute("friendlyName","International Standard Book Number")]
public string ISBN { get; set; }
}
序列化时应该产生类似这样的结果:
<Book>
<Title>To Kill a Mockingbird</Title>
<ISBN friendlyName="International Standard Book Number">9780061120084</ISBN>
</Book>
没有一个属性可以做到这一点。将元素文档与 xml 数据混合使用并不常见。通常,您希望使用 xsd 文档或其他方式记录您的 xml 模式。
话虽这么说,但这是您可以做到的。您需要将 ISBN
属性 从字符串更改为具有可以序列化的 friendlyName
属性 的自定义类型。
public class Book
{
public string Title { get; set; }
public ISBN ISBN { get; set; }
}
public class ISBN
{
[XmlText]
public string value { get; set; }
[XmlAttribute]
public string friendlyName { get; set; }
}
以下内容将完全按照您的问题进行序列化。
Book b = new Book
{
Title = "To Kill a Mockingbird",
ISBN = new ISBN
{
value = "9780061120084",
friendlyName = "International Standard Book Number",
}
};
更新
好的,另一种方法是创建一个自定义 XmlWriter
,它可以拦截序列化程序为创建元素所做的调用。当为要添加友好名称的 属性 创建元素时,您可以在自己的自定义属性中写入。
public class MyXmlTextWriter : XmlTextWriter
{
public MyXmlTextWriter(TextWriter w)
: base(w)
{
}
public override void WriteStartElement(string prefix, string localName, string ns)
{
base.WriteStartElement(prefix, localName, ns);
switch(localName)
{
case "ISBN":
WriteAttributeString("friendlyName", "International Standard Book Number");
break;
}
}
}
这是一个如何使用它的示例(来自控制台应用程序):
XmlSerializer serializer = new XmlSerializer(typeof(Book));
serializer.Serialize(new MyXmlTextWriter(Console.Out), b);
如果您需要能够写入其他内容,例如 Stream
。
XmlTextWriter
的其他构造函数
试试这个
[XmlRoot("Book")]
public class Book
{
[XmlElement("Title")]
public string Title { get; set; }
[XmlElement("ISBN")]
public ISBN isbn { get; set; }
}
[XmlRoot("ISBN")]
public class ISBN
{
[XmlAttribute("friendlyName")]
public string friendlyName { get; set; }
[XmlText]
public string value { get; set; }
}