简体   繁体   English

将类转换为XML的最快方法是什么

[英]What is the fastest way to convert a class to XML

I would like to know what is the fastest and most lightweight technique to convert a fairly large class to XML. 我想知道什么是将相当大的类转换为XML的最快,最轻量的技术。 The class will have lists and arrays in it. 该类将具有列表和数组。 I need to convert all this data to XML 我需要将所有这些数据转换为XML

Here is what my application does: it will get all the information from the database using linq to enties. 这是我的应用程序所做的事情:它将使用linq to enties从数据库中获取所有信息。 Then store the data in a class. 然后将数据存储在一个类中。 Then I want to convert this class to XML. 然后,我想将此类转换为XML。 When the data is in XML I will send the XML to the browser along with the xsl stylesheet to be displayed to the user. 当数据为XML格式时,我会将XML以及xsl样式表发送给浏览器,以显示给用户。 What is the fastest way to do this. 最快的方法是什么?

The XmlSerializer actually creates an assembly (with an XmlSerializationWriter ) that is custom made to serialize your class. XmlSerializer实际上创建了一个程序集(带有XmlSerializationWriter ),该程序集是定制的以序列化您的类。 You can look at the generated code by following these steps . 您可以按照以下步骤查看生成的代码。

You only pay the price the first time it encounters a new type. 您只需在第一次遇到新类型时支付价格。

So I think that you should really go with the XmlSerializer , not only for performance, but for maintainability. 因此,我认为您确实应该使用XmlSerializer ,这不仅是为了提高性能,而且是为了实现可维护性。

You can use a mixin-like serializer class: 您可以使用类似于mixin的序列化器类:

public interface MXmlSerializable { } 
public static class XmlSerializable {
  public static string ToXml(this MXmlSerializable self) {     
    if (self == null) throw new ArgumentNullException();     
    var serializer = new XmlSerializer(self.GetType());     
    using (var writer = new StringWriter()) {       
      serializer.Serialize(writer, self);       
      return writer.GetStringBuilder().ToString();     
    }   
  }   
}

public class Customer : MXmlSerializable {   
  public string Name { get; set; }   
  public bool Preferred { get; set; } 
}

// ....

var customer = new Customer {    
  Name = "Guybrush Threepwood",    
  Preferred = true }; 
var xml = customer.ToXml();

The fastest way is to write the code for it yourself. 最快的方法是自己编写代码。 That will remove any overhead, like the need to use reflection to read the properties of the object, as you can access the properties directly. 这将消除任何开销,例如需要使用反射来读取对象的属性,因为您可以直接访问属性。

Add a method to the class that returns it's data as XML, either by returning an XDocument, the XML already formatted as a string, or you can pass an XmlWriter to the method. 将方法添加到类中,以返回XML数据,方法是返回XDocument(已格式化为字符串的XML),也可以将XmlWriter传递给该方法。

By "fastest" do you mean you want the approach which will be fastest to develop? “最快”是指您要开发最快的方法吗? Or do you want the approach which will have the fastest execution speed? 还是您想要执行速度最快的方法?

If it's the former, I recommend just using .NET's XmlSerializer class: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx 如果是前者,我建议仅使用.NET的XmlSerializer类: http : //msdn.microsoft.com/zh-cn/library/system.xml.serialization.xmlserializer.aspx

Serializing a class to XML output is as simple as: 将类序列化为XML输出非常简单:

XmlSerializer serializer = new XmlSerializer(myObject.GetType());
serializer.Serialize(Response.OutputStream, myObject);

And there are various attributes you can decorate your class with to control things like whether individual properties are serialized as elements or attributes, etc. 您可以使用各种属性来修饰类,以控制诸如将单个属性序列化为元素还是属性等。

There's a good FAQ at http://www.devolutions.net/articles/serialization.aspx also http://www.devolutions.net/articles/serialization.aspx上也有很好的FAQ。

You could use XML serialization, for example: 您可以使用XML序列化,例如:

Foo foo = new Foo();
XmlSerializer serializer = new XmlSerializer(typeof(Foo));
TextWriter writer = new StringWriter();
serializer.Serialize(writer, foo);
string xml = writer.ToString();

The fastest method would depend on the class, because it would be hand-written to take advantage of knowledge of the specifics of that class in ways a more general approach couldn't do. 最快的方法将取决于类,因为它将被手写以利用更通用的方法无法做到的方式来利用该类的特定知识。

I'd probably use XmlTextWriter rather than straight to TextWriter though. 我可能会使用XmlTextWriter而不是直接使用TextWriter。 While the latter would allow for some further savings, these would be minimal compared to the better structure of XmlTextWriter, and you've already sacrificed a good bit in terms of structure and ease of maintenance as it is. 尽管后者可以带来更多节省,但是与XmlTextWriter的更好结构相比,这些节省是最小的,并且您已经在结构和易于维护方面牺牲了很多。

You can always slot in your super-optimised implementation of XmlWriter afterwards ;) 之后,您始终可以在XmlWriter的超级优化实现中添加插槽;)

It sounds like a rather convoluted set-up, when you could just display the class's information on a webpage using ASP.NET MVC. 当您仅可以使用ASP.NET MVC在网页上显示类的信息时,这听起来像是一个相当复杂的设置。 Why take the extra two steps of converting it to XML, send it to the browser, and use an XSL stylesheet to display it to the user? 为什么还要执行另外两个步骤,将其转换为XML,将其发送到浏览器,并使用XSL样式表将其显示给用户? It doesn't make sense. 这没有道理。

I wrote a program that serialized one simple object graph to XML in different ways: 1. Using XmlSerializer 2. Using hardcoded xml serializer 我编写了一个程序,以不同的方式将一个简单的对象图序列化为XML:1.使用XmlSerializer 2.使用硬编码的xml序列化器

30,000 documents: XmlSerializer took : 0.9 sec Hardcoded serializer took: 0.45 sec 30,000个文档:XmlSerializer花费了:0.9秒Hardcoded序列化器花费了:0.45秒

I relied on XmlWriter in both cases and that adds some overhead. 在这两种情况下,我都依赖XmlWriter,这增加了一些开销。

Note that you can instruct Visual Studio to generate the XmlSerializer assembly during compile time in order to reduce the serialization for that first instance (otherwise an assembly is generated in runtime). 请注意,您可以指示Visual Studio在编译时生成XmlSerializer程序集,以减少该第一个实例的序列化(否则在运行时中生成程序集)。

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

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