views:

136

answers:

7

I would like to know what is the fastest and most lightweight technique to convert a fairly large class to XML. The class will have lists and arrays in it. I need to convert all this data to XML

Here is what my application does: it will get all the information from the database using linq to enties. Then store the data in a class. Then I want to convert this class to 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. What is the fastest way to do this.

A: 

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

Serializing a class to XML output is as simple as:

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

Carson63000
I mean execution speed. My application will work with xml throughout. With a single page view I will be converting at least 5 classes to xml.
Luke101
Go with Guffa's recommendation, then, just handwrite some code in each class to spit out XML
Carson63000
A: 

You could use XML serialization, for example:

Foo foo = new Foo();
XmlSerializer serializer = new XmlSerializer(typeof(Foo));
TextWriter writer = new StringWriter();
serializer.Serialize(writer, foo);
string xml = writer.ToString();
Richard Hein
+2  A: 

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.

Guffa
Good idea..would you happen to have an example of this or a link that shows how to do this?
Luke101
If I understand XmlSerializer correctly, it uses Reflection.Emit to generate code that directly accesses the data, then saves the result to disk. Reflection isn't used for each object. The whole process is probably not much slower than compiling the hand-written code, and then you can deploy the resulting assembly alongside your own code.
Ben Voigt
A: 

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. 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.

You can always slot in your super-optimised implementation of XmlWriter afterwards ;)

Jon Hanna
A: 

It sounds like a rather convoluted set-up, when you could just display the class's information on a webpage using 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? It doesn't make sense.

George Stocker
+4  A: 

The XmlSerializer actually creates an assembly (with an XmlSerializationWriter) that is custom made to serialize your class. 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.

You can use a mixin-like serializer class:

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();
Jordão
A: 

I wrote a program that serialized one simple object graph to XML in different ways: 1. Using XmlSerializer 2. Using hardcoded xml serializer

30,000 documents: XmlSerializer took : 0.9 sec Hardcoded serializer took: 0.45 sec

I relied on XmlWriter in both cases and that adds some overhead.

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).

FuleSnabel