将 XML 文件加载到 VB.Net 结构中

发布于 2024-09-15 17:18:29 字数 611 浏览 4 评论 0原文

我有许多(15-20)个不同的 XML 文件需要加载到 VB.Net。它们的设计就像在数据库中一样;它们在 Access 中设计并批量导出到 XML 文件中。 每个文件代表数据库中的一个不同的表。

现在,我需要将此信息加载到 VB.Net 中。最初,我很想使用 DAO 并通过查询直接访问 MDB,但这不可能,因为我要确保项目能够轻松移植到XNA/C#(Xbox 360 无法使用 MDB,所以我宁愿现在就解决这个问题,而不是以后再解决)。

因此,我现在陷入困境,试图弄清楚如何将所有这些 XML 文件整理在一起一起。我尝试过使用工厂来单独解析每个工厂。例如,如果三个 XML 文件包含“字符”类的数据,我会将一个字符实例传递给每个 XML 工厂,并且这些类将应用必要的数据。

不过,我正在努力克服这个问题,因为用冗余代码维护许多不同的类是一件痛苦的事情。而且它也很难调试。所以我正在尝试找出新的解决方案。

我现在唯一能想到的是使用 System.Reflection,在其中解析我正在实例化的类/结构的每个成员,然后使用这些成员的名称从 XML 的该元素中读取数据文件。

然而,这假设结构/类的每个成员在 XML 文件中都有一个匹配的元素,反之亦然。

I have many, (15-20) different XML files that I need to load to VB.Net. They're designed as they would be in a database; they're designed in Access and bulk exported into XML files. Each file represents a different table in the database.

Now, I need to load this information into VB.Net. Initially, I'd love to use DAO and access the MDB directly via queries, but this won't be possible as I'm making sure the project will be easily ported to XNA/C# down the road. (Xbox 360 cannot use MDBs, so I'd rather deal with this problem now than down the road).

So, I'm stuck now trying to figure out how to wrangle together all of these XML files together. I've tried using Factories to parse each one individually. E.g., if three XML files contain data for a 'character' class, i'd pass in an instance of Character to each XML factory and the classes would apply the necessary data.

I'm trying to get past this though, as maintaining many different classes with redundant code is a pain. plus it is hard to debug as well. So I'm trying to figure out a new solution.

The only thing I can think of right now is using System.Reflection, where I parse through each member of the class/structure I'm instantiating, and then using the names of those members to read in the data from that element of the XML file.

However, this makes the assumption that each member of the structure/class has a matching element in the XML file, and vice-versa.

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

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

发布评论

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

评论(2

久夏青 2024-09-22 17:18:29

如果您知道 XML 文件的架构 - 您可以创建 .NET 类,将这些 XML 文件之一反序列化为 .NET 对象的实例。

如果您有 XSD 文件(或者编写 XSD 比编写可序列化的 .NET 类更容易),您还可以使用 xsd.exe(随 Windows SDK 下载一起提供)生成 .NET 类定义。

If you know the schema of the XML files - you could create .NET classes that can deserialize one of those XML files into an instance of a .NET object.

You can also you use xsd.exe (comes with Windows SDK download) to generate the .NET class definition for you if you have an XSD file (or can write an XSD easier than you can write a serializable .NET class).

假装不在乎 2024-09-22 17:18:29

Linq-to-XML 是一个很好的解决方案(并且 在 VB.NET 中效果更好,例如 XML 文字和全局命名空间)。有时,将多个 XML 文件视为数据库表可能会很困难,但并非不可能。我想我会从 JOIN 开始(即使它有“标题中的 C#”,示例也是用 VB 编写的)..

Linq-to-XML is a good solution (and even better in VB.NET with things like XML Literals and Global Namespaces). Treating multiple XML files as DB tables can be a rough road some times, but certainly not impossible. I guess I'd start with JOIN (even though it has "C#" in the title, the samples are also in VB)..

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