简体   繁体   English

有没有办法将ac#class序列化为xml而不在类中放置属性?

[英]Is there a way to serialize a c# class to xml without put attributes on the class?

I would like to serialize ac# class but I do not want to put the attributes on the class. 我想序列化ac#类,但我不想把属性放在类上。 I've found FluentXML on codeplex, but it seems to be just an idea project and is not working anyway 我在codeplex上找到了FluentXML,但它似乎只是一个想法项目而且无论如何都无法正常工作

Here is an example that worked for me. 这是一个适合我的例子。 From Serialize object to string Serialize对象到字符串

I just googled "serialize class to xml string" 我只是用谷歌搜索“序列化类到xml字符串”

String XmlizedString = null;

XmlSerializer x = new XmlSerializer(objectNameHere.GetType());

MemoryStream memoryStream = new MemoryStream();

XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

x.Serialize(xmlTextWriter, objectNameHere);

memoryStream = (MemoryStream)xmlTextWriter.BaseStream;

UTF8Encoding encoding = new UTF8Encoding();

XmlizedString = encoding.GetString(memoryStream.ToArray());

XmlizedString = XmlizedString.Substring(1);

It produced a string like this <?xml version="1.0" encoding="utf-8"?><Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Name>StackOverflow User</Name><Title>User of StackOverflow.</Title><Salary>70000</Salary><Skills><string>Being Awesome</string><string>Being sweet</string></Skills></Employee> 它生成了一个像这样的字符串<?xml version="1.0" encoding="utf-8"?><Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Name>StackOverflow User</Name><Title>User of StackOverflow.</Title><Salary>70000</Salary><Skills><string>Being Awesome</string><string>Being sweet</string></Skills></Employee>

From an Employee class as simple as this: 从Employee类开始,就像这样简单:

public class Employee
{
  public string Name { get; set; }
  public string Title { get; set; }
  public double Salary { get; set; }
  public Collection<string> Skills { get; set; }
}

There's nothing forcing you to put attributes on your class in order to use XmlSerializer . 没有什么可以强迫你在你的类上放置属性来使用XmlSerializer In this case the generated XML will simply reflect your object structure. 在这种情况下,生成的XML将简单地反映您的对象结构。 If on the other hand you want to control the generated XML you could use a XmlWriter or an XDocument instead. 另一方面,如果您想控制生成的XML,则可以使用XmlWriterXDocument But the attributes are designed exactly for that purpose and you should use them. 但是属性是为此目的而设计的,您应该使用它们。 If for some reason you cannot modify the class you are serializing (because for example you do not have the source code for it) you could always design a different class which you could decorate with attributes and then map the original class to this model that you would serialize. 如果由于某种原因你不能修改你正在序列化的类(因为你没有它的源代码)你总是可以设计一个不同的类,你可以用属性装饰,然后将原始类映射到这个模型,你会序列化。

您可以使用没有属性的XmlSerializer ,并使用它为您提供的默认名称,或者您也可以在类上实现IXmlSerializable接口,以获得更加自定义但手动输出,而无需使用属性。

假设你有一个很好的理由不使用属性,你可以使用XmlSerializer ,但要注意它只适用于公共成员,还需要一个公共类型和一个无参数构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM