如何从一个DataTable写时指定XML输出​​的格式? [英] How to specify format of XML output when writing from a DataTable?

查看:167
本文介绍了如何从一个DataTable写时指定XML输出​​的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我创建从一个DataTable中使用dataTable.WriteXml(文件路径)一个XML文件,并得到如下:

 < XML版本=1.0编码=UTF-8&GT?;
< ExperienceProfiles>
  < ExperienceProfile>
    < COL1>等等< / COL1>
    < col2的> 4ed397bf-a4d5-4ace-9d44-8c1a5cdb0f34< / col2的>
  < / ExperienceProfile>
< / ExperienceProfiles>
 

怎样才能得到它写在以下格式的XML:

 < XML版本=1.0编码=UTF-8&GT?;
< ExperienceProfiles>
  < ExperienceProfile COL1 =嗒嗒
    COL2 =嗒嗒>< / ExperienceProfile>
< / ExperienceProfiles>
 

解决方案

你需要的是某种方法来告诉数据集数据的预期格式。你很幸运,该数据集只支持此功能。

您需要为您的数据的XML模式,并加载到数据集,你写出来的XML之前。在模式中定义COL1和col2作为ExperienceProfile元素的属性和数据集就知道格式化输出文件以满足方案规定的要求。

如果您不熟悉创建可以创建XML的一个示例文件,你希望它被格式化的方式,再看看在该框架的XmlSchemaInference类的模式。这个类可以用于自动生成您的架构,你可能需要调整输出一点点,但它可以帮助,如果你不熟悉的XSD。

In C#, I'm creating an XML file from a DataTable using dataTable.WriteXml(filePath), and get the following:

<?xml version="1.0" encoding="utf-8" ?>
<ExperienceProfiles>
  <ExperienceProfile>
    <Col1>blah</Col1>
    <Col2>4ed397bf-a4d5-4ace-9d44-8c1a5cdb0f34</Col2>
  </ExperienceProfile>
</ExperienceProfiles>

How can I get it to write the XML in the following format?:

<?xml version="1.0" encoding="utf-8" ?>
<ExperienceProfiles>
  <ExperienceProfile Col1="blah"
    Col2="blah" ></ExperienceProfile>
</ExperienceProfiles>

解决方案

What you want is some way to tell the DataSet the expected format of your data. You're in luck, the DataSet supports just this feature.

You will need to create an XML schema for your data and load it into the DataSet before you write out the XML. In the schema define Col1 and Col2 as attributes of the ExperienceProfile element and the DataSet will know to format the output document to meet the requirements specified in the schema.

If you are not comfortable with creating a schema you can create a sample of the XML file the way that you want it to be formatted, then take a look at the XmlSchemaInference class in the framework. This class can be used to automatically generate your schema, you may need to tweak the output a little but it can help if you are not familiar with XSD.

这篇关于如何从一个DataTable写时指定XML输出​​的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆