外部 Xml 到 C# 对象,最简单的方法是什么?

发布于 2024-12-01 16:41:17 字数 806 浏览 0 评论 0原文

我们从外部程序生成了 XML,需要将其读入我们的 C# 应用程序。

我知道我们可以使用带有 [Serializing] 标记的 C# 类来将其加载到对象中,但问题是我们已经拥有了比 Xml 更加面向对象的对象。我们不想有 2 组对象。

我的想法是,我可以使用 LinqToXml 打开该文件并填充我们的对象。这样做可以吗?或者真正的最佳方法是在我们的 OO 模型中使用带有 XmlElementAttribute[Serializing] 标记和其他标记? (如果最新的是好的解决方案,我如何将属性放在对象内部的对象中或使用继承?)。

编辑:示例

假设我有:

public class Movie {
    public string Name;
    public Actor LeadActor;
}

public class Actor {
    public string Name;
    public DateTime DOB;
}

并且我想像这样序列化电影:

<Movie>
    <Name>Casino Royale</Name>
    <LeadActor>Daniel Craig</LeadActor>
</Movie>

我认为使其工作的唯一方法是修改电影以具有 LeadActor 属性,该属性将连接 Actor 属性并使两个 Actor 属性与[XmlIgnore] 属性。但是如果我不想改变这些业务对象怎么办?

We have XML generated from an external program that needs to be read into our C# application.

I know we could use C# classes with [Serializable] tag to load the into objects but the problem is that we already have our object that is really more OO divised than the Xml. We do not want to have 2 sets of objects.

My though is that I could open the file with LinqToXml and fill up our objects. Is it okay to do it this way or the real best way is to use the [Serializable] tag with XmlElementAttribute and other tag into our OO model? (If the latest is the good solution, how can I put an attribute in object inside my object or to work with inheritance?).

Edit : Example

Let say that I have :

public class Movie {
    public string Name;
    public Actor LeadActor;
}

public class Actor {
    public string Name;
    public DateTime DOB;
}

And I want to serialize the Movie like this:

<Movie>
    <Name>Casino Royale</Name>
    <LeadActor>Daniel Craig</LeadActor>
</Movie>

The only way I see to make it works is to modify the Movie to have a LeadActor property that will concatenare the Actor property and make both Actor property with [XmlIgnore] attribute. But what if I do not want to alter those business object?

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

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

发布评论

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

评论(4

很糊涂小朋友 2024-12-08 16:41:17

如果 xml 数据的结构已知(并且不太复杂),我发现使用 Xml.Linq 是最简单的方法。每个类都包含公共静态方法来从 xml 重建自身。您必须手动执行此操作,但如果 xml 结构与对象结构不对应,这似乎是唯一的方法 - 而且这个方法非常简单。
对于您的示例,它将是:

public class Movie {
    public string Name;
    public Actor LeadActor;
    public static Movie FromXml (XElement x)
    {
        Movie m = new Movie();
        m.Name = x.Element("Name").Value;
        m.LeadActor = Actor.FromXml(x.Element("LeadActor"));
        return m;
    }
}

public class Actor {
    public string Name;
    public DateTime DOB;
    public static Actor FromXml (XElement x)
    {
        Actor a = new Actor();
        a.Name = x.Value;
        a.DOB = ... 
        return a;
    }
}

If the structure of the xml data is known (and not too complex) I find using Xml.Linq the easiest way. Each class contains public static method to reconstruct itself from xml. You have to do it manually, but if the xml structure does not correspond to object structure that seems like the only way - and this one is quite simple.
For your example it would be:

public class Movie {
    public string Name;
    public Actor LeadActor;
    public static Movie FromXml (XElement x)
    {
        Movie m = new Movie();
        m.Name = x.Element("Name").Value;
        m.LeadActor = Actor.FromXml(x.Element("LeadActor"));
        return m;
    }
}

public class Actor {
    public string Name;
    public DateTime DOB;
    public static Actor FromXml (XElement x)
    {
        Actor a = new Actor();
        a.Name = x.Value;
        a.DOB = ... 
        return a;
    }
}
无敌元气妹 2024-12-08 16:41:17

[Serializable] 属性启用二进制序列化,而不是 Xml 序列化。要支持 XML 序列化,对象必须具有默认构造函数和公共可设置属性。如果您的对象模型满足此要求,我会考虑使用内置的 XML 序列化 - 从长远来看它可以节省时间。

[Serializable] attribute enabled binary serialization, not Xml serialization. To support XML serialization object must have default constructor and public settable properties. If your object model meets this requirement I would consider using built-it XML serialization - it saves time in the long run.

饮惑 2024-12-08 16:41:17

您可以在顶级对象上实现 IXmlSerialized进行一些自定义序列化。

You could implement IXmlSerializable on your top level object to do some custom serialization.

百合的盛世恋 2024-12-08 16:41:17

您可以使用 xslt 转换将源 xml 转换为与对象模型匹配的 xml 格式,然后直接将转换后的 xml 反序列化为对象。

You could use an xslt transformation to transform the source xml into the xml format that matches your object model, and then directly deserialize the transformed xml into objects.

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