C# 反序列化 Xml 为对象并再次序列化回 Xml

发布于 2024-11-30 14:20:09 字数 414 浏览 1 评论 0原文

我想使用 JsonFx 将 XML 与自定义类型和 LINQ 查询相互转换。谁能提供一个反序列化和再次序列化的例子吗?

下面是我正在使用的 XML 示例。 XML 粘贴在这里: http://pastebin.com/wURiaJM2

JsonFx 支持将 json 绑定到 .net 对象的多种策略,包括动态对象。 https://github.com/jsonfx/jsonfx

亲切的问候 PS

我确实尝试将 xml 文档粘贴到 StackOverflow 中,但它删除了很多文档引用和 XML 声明。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女 2024-12-07 14:20:09

这是我用过的一个方法。它可能需要一些调整:

    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();
    }

对于反序列化:

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

并这样称呼它:

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

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

希望这会有所帮助。 Serialize 方法中的 rootName 参数允许您自定义生成的 xml 字符串中根节点的值。此外,您的类必须使用正确的 Xml 属性进行修饰,这些属性将控制实体的序列化方式。

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.

最好是你 2024-12-07 14:20:09

这篇文章解释了如何从 XML 文件创建 XSD 和类,然后介绍序列化和反序列化。
http://geekswithblogs.net/CWeeks/archive/2008/03/11 /120465.aspx

使用此技术与 XSD.exe 创建 XSD,然后在 CS 文件中创建类,我能够序列化,然后再次反序列化。

然而,序列化过程不会创建源 XML 的精确表示,因此仍然需要完成一些后期工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文