Reputation: 62804
I have stumbled across http://www.sharpserializer.com/en/index.html, but unfortunately the XML output it produces is formatted in a somewhat peculiar fashion, which makes it unsuitable for cross platform communication.
I have got fed up with the standard .NET XML serializer, so I am looking for an alternative.
Specifically, I would like it to be as potent as the Newtonsoft.Json serializer, but for XML, of course.
Thanks.
EDIT
There are two problems with the standard .NET XML serializer:
Yes, you can workaround by defining proxy members and all that. Why don't we have any problems with Newtonsoft.Json when doing json serialization?
Upvotes: 0
Views: 1080
Reputation: 3401
Check out DataContract Serializer - it's used by WCF internally to serialize/deserialize SOAP-ed objects.
using System.Runtime.Serialization;
/// <summary>
/// Returns object serialized as an XML string by using DataContractSerializer
/// </summary>
/// <returns></returns>
public override string ToString()
{
DataContractSerializer serializer = new DataContractSerializer(this.GetType());
System.IO.MemoryStream stream = new System.IO.MemoryStream();
serializer.WriteObject(stream, this);
return System.Text.Encoding.ASCII.GetString(stream.ToArray());
}
Upvotes: 0
Reputation: 28178
You want to implement IXmlSerializable on your class, this will require you to add 3 methods : 1) ReadXml 2) WriteXml 3) GetSchema
Here you can specify exactly how your files serialize and deserialize.
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
public class Person : IXmlSerializable
{
// Private state
private string personName;
// Constructors
public Person (string name)
{
personName = name;
}
public Person ()
{
personName = null;
}
// Xml Serialization Infrastructure
public void WriteXml (XmlWriter writer)
{
writer.WriteString(personName);
}
public void ReadXml (XmlReader reader)
{
personName = reader.ReadString();
}
public XmlSchema GetSchema()
{
return(null);
}
// Print
public override string ToString()
{
return(personName);
}
}
Heres a link to the docs. http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx
Upvotes: 1