简体   繁体   English

c#如何从数据集或XML文件创建类型化的数据集?

[英]c# how to create a typed dataset from a dataset or a XML file?

I have XML file that I can easily load into a dataset. 我有可以轻松加载到数据集中的XML文件。

    DataSet ds = new DataSet();
    ds.ReadXml(e.FullPath);

However, I did like to have a typed dataset instead. 但是,我确实想拥有一个类型化的数据集。
Is there a way to build the typed dataset from the XML or the untyped dataset? 有没有一种方法可以从XML或非类型化数据集构建类型化数据集?

The goal here is to avoid design it because there is a lot of column and it remove the change of making mistake while typing. 这里的目标是避免设计它,因为有很多列,并且它消除了键入时犯错误的更改。

It is really easy to do. 这真的很容易做到。 Here is a step by step guide. 这是逐步指南。
(You must have visual studio installed of course) (当然,您必须安装了Visual Studio)

1. From the XML File, create a XSD File. 1.在XML文件中,创建一个XSD文件。

This can be acheived by loading the XML file into a dataset and then writing the schema. 可以通过将XML文件加载到数据集中,然后编写架构来实现。 The following code take care of this step. 以下代码负责此步骤。

        DataSet ds = new DataSet();   
        ds.ReadXml(e.FullPath);   
        ds.WriteXml(@"C:\test.xsd", XmlWriteMode.WriteSchema);

Verify the XSD file is good and doesn't contains junk. 验证XSD文件是否正确,并且其中不包含垃圾内容。

2. Locate where "xsd.exe" is on your computer. 2.找到“ xsd.exe”在计算机上的位置。
In my case, it was here : 就我而言,它在这里:
C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v8.0A\\bin\\NETFX 4.0 Tools C:\\ Program Files(x86)\\ Microsoft SDKs \\ Windows \\ v8.0A \\ bin \\ NETFX 4.0工具

3. Copy/paste the xsd file into the folder found at step2. 3.将xsd文件复制/粘贴到在步骤2找到的文件夹中。

4. Run command prompt and change directory to folder found at step2. 4.运行命令提示符,并将目录更改为在步骤2中找到的文件夹。

Open a command prompt (Windows+R, type "cmd" and hit enter). 打开命令提示符(Windows + R,键入“ cmd”,然后按Enter键)。 Then navigate to the folder found at step2. 然后导航到在步骤2找到的文件夹。 ("cd enter-your-directory-here") (“ cd在此处输入您的目录”)

    cd C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools  

Quick tips, you can copy/paste the directory into the prompt using the mouse's right click. 快速提示,您可以使用鼠标右键单击将目录复制/粘贴到提示中。

5. From the command prompt, launch xsd.exe with desired argument. 5.在命令提示符下,使用所需参数启动xsd.exe。
Type or paste the following command in the command prompt and press enter. 在命令提示符下键入或粘贴以下命令,然后按Enter。

    xsd.exe /d /l:cs test.xsd /eld /n:MyDesiredNameSpace  

If needed, you can find details about arguments in msdn documentation. 如果需要,可以在msdn文档中找到有关参数的详细信息

Given your xsd file was valid, it will output a cs file into the folder. 鉴于您的xsd文件有效,它将在文件夹中输出一个cs文件。 Here is what console output. 这是控制台输出。

命令输出

6. Include the generated file into your project. 6.将生成的文件包含到您的项目中。
Copy and paste the generated .cs file into your solution folder and include it to your project. 将生成的.cs文件复制并粘贴到解决方案文件夹中,并将其包含在您的项目中。 Using (right-click include existing item) 使用(右键单击包括现有项目)
将文件包含到项目中

7. Rename the typed dataset with an appropriate name. 7.用适当的名称重命名类型化的数据集。
The generated typed dataset is "NewDataset". 生成的类型化数据集为“ NewDataset”。
Rename it for a more decent name. 重命名为更体面的名称。 (use refactor feature) (使用重构功能) 重命名类型化的数据集

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM