开发者

C# de-serialise Xml to object and serialise back to Xml again

I would like to use JsonFx to convert XML to/from custom types and LINQ queries. Can anyone please provide an example to de-serialisation and serialisation back again?

Here's an example of the XML I'm working with. XML 开发者_如何学Cpasted here: http://pastebin.com/wURiaJM2

JsonFx Supports several strategies of binding json to .net objects including dynamic objects. https://github.com/jsonfx/jsonfx

Kind regards Si

PS I did try pasting the xml document into StackOverflow but it removed a lot of the documents quotes and XML declaration.


Here's a method that I have used. It may require some tweaking:

    public static string SerializeObject<T>(T item, string rootName, Encoding encoding)
    {

        XmlWriterSettings writerSettings = new XmlWriterSettings();
        writerSettings.OmitXmlDeclaration = true;
        writerSettings.Indent = true;
        writerSettings.NewLineHandling = NewLineHandling.Entitize;
        writerSettings.IndentChars = "    ";
        writerSettings.Encoding = encoding;

        StringWriter stringWriter = new StringWriter();

        using (XmlWriter xml = XmlWriter.Create(stringWriter, writerSettings))
        {

            XmlAttributeOverrides aor = null;

            if (rootName != null)
            {
                XmlAttributes att = new XmlAttributes();
                att.XmlRoot = new XmlRootAttribute(rootName);

                aor = new XmlAttributeOverrides();
                aor.Add(typeof(T), att);
            }

            XmlSerializer xs = new XmlSerializer(typeof(T), aor);

            XmlSerializerNamespaces xNs = new XmlSerializerNamespaces();
            xNs.Add("", "");

            xs.Serialize(xml, item, xNs);
        }

        return stringWriter.ToString();
    }

And for Deserialization:

    public static T DeserializeObject<T>(string xml)
    {
        using (StringReader rdr = new StringReader(xml))
        {
            return (T)new XmlSerializer(typeof(T)).Deserialize(rdr);
        }
    }

And call it like this:

string xmlString =  Serialization.SerializeObject(instance, "Root", Encoding.UTF8);

ObjectType obj = Serialization.DeserializeObject<ObjectType>(xmlString);

Hope this helps. The rootName parameter in the Serialize method lets you customize the value of the root node in the resulting xml string. Also, your classes must be decorated with the proper Xml attributes which will control how an entity is serialized.


This post explains how to create an XSD and a Classes from an XML file and then covers serialisation and de-serialisation. http://geekswithblogs.net/CWeeks/archive/2008/03/11/120465.aspx

Using this technique with the XSD.exe to create an XSD and then classes in a CS file I was able to serialisation and then de-serialisation back again.

However the serialisation process does not create an exact representation of the source XML, so there's still some post work to be done there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜