如何使用 XmlSerializer 类反序列化这个简单的 xml 配置?

发布于 2024-07-08 16:31:54 字数 930 浏览 7 评论 0原文

我有以下 xml,我想反序列化为一个类,

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <element1>String1</element1>
  <element2>String2</element2>
</root>

我试图将其序列化为以下类:

    [XmlRoot("root")]
    public class root
    {
        [XmlElement("element1")]
        internal string element1 { get; set; }

        [XmlElement("element2")]
        internal string element2 { get; set; }
    }

当我尝试使用以下代码对其进行反序列化时,配置对象被实例化,但字符串为空。

     using (TextReader reader = new StreamReader(configFile))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(root));
            this.config = (root)serializer.Deserialize(reader);
        }

我尝试使用 xsd.exe 创建一个 xsd,然后创建一个基于该类的类,但是该工具生成了太多混乱。 我想我就在这儿附近。 我缺少什么?

I have the following xml I'd like to deserialize into a class

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <element1>String1</element1>
  <element2>String2</element2>
</root>

I am trying to serialize it into the following class:

    [XmlRoot("root")]
    public class root
    {
        [XmlElement("element1")]
        internal string element1 { get; set; }

        [XmlElement("element2")]
        internal string element2 { get; set; }
    }

When I try deserializing it using the following code, the config object is instantiated, but the strings are null.

     using (TextReader reader = new StreamReader(configFile))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(root));
            this.config = (root)serializer.Deserialize(reader);
        }

I've tried using xsd.exe to create an xsd, and then create a class based off of that, but there is too much clutter generated by that tool. I think I'm close here. What am I missing?

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

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

发布评论

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

评论(4

终陌 2024-07-15 16:31:54

您不能序列化/反序列化内部属性 - 它们必须是公共的。

You can't serialise/deserialise internal properties - They have to be public.

萌辣 2024-07-15 16:31:54

我同意布罗迪关于你问题的性质的看法。 但是,您可能反对公开这些字段。 我过去处理这个问题的方法是创建一个可序列化的类,其唯一目的是读/写 .xml 并将其所有字段公开。 然后创建一个新的类作为外部接口。 它将可序列化类作为构造函数的参数,外部类提供控制对可序列化类的访问的公共属性。

I concur with Brody as to the nature of your problem. However, you may have an objection to making these fields public. The way I have handled this problem in the past is to create a serializable class whose only purpose is to read/write .xml and has all of its fields public. Then create a new class which is the external interface. It takes the serializable class as an argument of a constructor and the external class provides public properties which controls the access to the serializable class.

赏烟花じ飞满天 2024-07-15 16:31:54

为了跟进我的实现...我最终放弃了使用 XmlSerializer 类。 我反序列化的类非常复杂,并且包含需要序列化的其他对象的列表。 我必须添加到类中的属性数量使得 代码很糟糕

我最终使用了 Linq到 XML 来进行反序列化......类声明的复杂性下降了,但 linq 语句最终变得相当复杂。

如果我再做一次,我可能会考虑使用 WCF 和数据契约序列化器......这也可能很难做到。

我很好奇现在人们如何将 xml 文档反序列化为对象。 在了解了 Linq 语句之后,我认为这可能是要走的路。 这些对象的创建要简单得多,并且不需要公开。 看起来 XmlSerializer 是“老派”,而 Linq to XML 则更“新派”。

我很想听听其他人怎么说。

To follow up on my implementation... I ended up abandoning using the XmlSerializer class all together. The classes I was deserializing were pretty complex and contained lists of other objects that needed to be serialized. The amount of attributes I had to add to my classes made the code stink

I ended up using Linq to XML to do the deserialization.... the complexity of the class delcarations went down, but the linq statement eneded up being rather complex.

If I were to do it again, I might have thought about using WCF and the datacontract serializer... That might have also been difficult to do also.

I'm curious how people are deserializing xml docs into objects these days. After getting my head around Linq statements, I think this might be the way to go. The objects are much simpler to create, and they don't need to be public. It also seems like the XmlSerializer is "old-school" while Linq to XML is more "new-school".

I'd love to hear what others had to say.

吻安 2024-07-15 16:31:54

您可以使用 XSD.exe 从 XSD(XML 架构定义)生成类。 这会生成一个可用的类结构,可以序列化和反序列化相关的 XML。

You can use XSD.exe to generate a class from an XSD (XML Schema Definition). This produces a usable class structure that can serialise and deserialise the relevant XML.

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