将 xml 架构和数据加载到 DataSet(和 datagridview)中

发布于 2024-12-10 18:22:05 字数 482 浏览 0 评论 0原文

我有一个 datagridview,其中填充了 .xml 文件中的数据。数据是 MyObjects 的列表,其中 MyObject 是我拥有的 C# 类。这最初是在没有模式的情况下完成的,因此没有类型信息:(意味着我没有在 MyObject 类中获得自动生成布尔值等复选框列的好处。

所以我使用 xsd.exe 生成 .xsd 文件看起来很棒!但是如何使用应用程序部署这个 .xsd ?我是否必须确保它与我的应用程序位于同一目录中并像这样加载:

DataSet ds = new DataSet();
ds.ReadXml("mystuff.xml");
ds.ReadXmlSchema("myschema.xsd");
dataGridView_1.DataSource = ds;
dataGridView_1.DataMember = "MyObject";

我确信有更好的方法来处理这个问题...能我将其作为程序集的一部分或其他内容包含在内?谢谢您的帮助。

I have a datagridview populated with data from an .xml file. The data is a list of MyObjects, where MyObject is a C# class I have. This initially was done without a schema, so no type information :( Means I don't get the benefit of auto-generated checkbox columns for bools, etc. in the MyObject class.

So I used xsd.exe to generate an .xsd file. Looks great! But how do I deploy this .xsd with the app? Do I have to make sure it sits in the same directory as my app and load it like:

DataSet ds = new DataSet();
ds.ReadXml("mystuff.xml");
ds.ReadXmlSchema("myschema.xsd");
dataGridView_1.DataSource = ds;
dataGridView_1.DataMember = "MyObject";

I'm sure there's a better way to handle this... can I include it as part of the assembly or something? Thanks for any help.

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

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

发布评论

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

评论(1

╰ゝ天使的微笑 2024-12-17 18:22:05

使其成为嵌入式强类型资源。

将文件添加到您的项目中,并为其指定“内容”和“不复制”的构建操作。然后打开“资源设计器”选项卡(从“属性”文件夹或“项目属性”对话框中)并将文件拖到资源设计器画布上。该文件现在已嵌入到您的程序集中。这将生成默认 Resources 类的强类型属性,其名称与其来源的文件相同。

要将其加载到数据集中,您可以将 StringReader 连接到它。请注意,您几乎总是希望首先加载架构,因为它会更改 ReadXml 的行为:

var ds = new DataSet();
using (var rdr = new StringReader(Properties.Resources.myschema))
{
    ds.ReadXmlSchema(rdr);
}
ds.ReadXml("mystuff.xml", XmlReadMode.IgnoreSchema);

对于 XmlReadMode,您有几个选项。它们规定了如果您的数据与您的架构不匹配时会发生什么,以及如果 XML 文件中内联定义的架构与您已加载的架构不同时该怎么办:

  • XmlReadMode.ReadSchema 将导入任何内联模式,只要它不与已加载的模式冲突;如果存在名称冲突,ReadXml 将抛出;或
  • XmlReadMode.IgnoreSchema 将忽略任何内联架构,并尝试将数据强制为您指定的架构。在此模式下,与您的架构不匹配的数据将不会最终出现在数据集中。
  • XmlReadMode.InferSchema 将忽略任何内联架构,但在这种情况下,任何不符合您的架构的数据都将导致您的架构被扩展;例如,如果您的 XML 文件有一个不在您的架构中的表,则该表将添加到您的架构中并导入数据。如果不同类型的列之间存在名称冲突,ReadXml 将抛出;

如果您首先执行 ReadXml,如果有内联架构,则始终会获得 ReadSchema 模式;如果没有,则始终会获得 InferSchema 模式。即使这就是您想要的,最好明确说明。

Make it an embedded strongly-typed resource.

Add the file to your project and give it a Build Action of "Content", and "Do Not Copy". Then open up the Resources designer tab (either from the Properties folder, or in the Project Properties dialog) and drag the file onto the Resource Designer canvas. The file is now embedded into your assembly. This will produce a strongly-typed property of the default Resources class, with the same name as the file it came from.

To load it into the data set you can hook up a StringReader to it. Note that you almost always want to load the schema first, as it changes the behavior of ReadXml:

var ds = new DataSet();
using (var rdr = new StringReader(Properties.Resources.myschema))
{
    ds.ReadXmlSchema(rdr);
}
ds.ReadXml("mystuff.xml", XmlReadMode.IgnoreSchema);

For XmlReadMode you have a couple of options. They dictate what happens if your data doesn't match your schema, and what to do if there's a schema defined inline in the XML file that differs from the one you already loaded:

  • XmlReadMode.ReadSchema will import any inline schema as long as it doesn't collide with the already-loaded schema; if there are name collisions, the ReadXml will throw; or
  • XmlReadMode.IgnoreSchema will ignore any inline schema, and try to force the data to the schema you specified. In this mode, data that doesn't match your schema will not end up in the dataset.
  • XmlReadMode.InferSchema will ignore any inline schema, but in this case, any data that doesn't conform to your schema will cause your schema to be extended; for example, if your XML file has a table that isn't in your schema, that table will get added to your schema and the data imported. If there are name collisions between columns of different types, ReadXml will throw;

If you do the ReadXml first, you always get ReadSchema mode if there's an inline schema, or InferSchema mode if there isn't. Even if that's what you want it's better to be explicit about it.

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