DataSet.WriteXml() - 如何“删除”一些领域
我有 2 个问题:
首先,我有一个包含 5 个表的数据集。我已经建立了表的关系并从该数据集生成了 XML,如下所示:
StreamWriter xmlDoc = new StreamWriter("myxml.xml", false);
ds.WriteXml(xmlDoc);
xmlDoc.Close();
数据集中每个表中的某些字段是主键,我不想在 XML 中显示它们。如果我将它们从表中排除,我就无法建立关系。有人能给我一些关于如何将数据集写入 XML 并“删除”关键字段(数据表中的列)的想法吗?例如,这是目前生成的 XML:
<?xml version="1.0"?>
<o>
<sp spname="SP1" spid="8">
<event spid="8" eventname="Event1" eventId="482">
<bm bmname="BM1" bmid="2" bmeid="826" eventid="482">
<att bmeid="826" val="3.00" attname="Att1" atttype="Type1" attid="23172"/>
<att bmeid="826" val="3.50" attname="Att2" bettype="Type1" attid="23173"/>
</bm>
</event>
</sp>
</o>
但我希望像这样生成此 XML(所有 id 属性都应“删除”,因为所有 id 都是用于关系的,不应在 XML 中添加):
<?xml version="1.0"?>
<o>
<sp spname="SP1">
<event eventname="Event1">
<bm bmname="BM1" bmid="2">
<att val="3.00" attname="Att1" atttype="Type1" />
<att val="3.50" attname="Att2" bettype="Type1" />
</bm>
</event>
</sp>
</o>
现在是第二个问题:
我已将数据集命名为“o”,因此它会生成 xml,如您在上面看到的那样。我想向
节点添加一些属性,例如当前日期时间。我的意思是我希望
节点生成为
。我怎样才能实现它?
谢谢,
I've 2 questions:
First, I've a dataset with 5 tables. I've made the relationships of tables and generating an XML from this dataset like this:
StreamWriter xmlDoc = new StreamWriter("myxml.xml", false);
ds.WriteXml(xmlDoc);
xmlDoc.Close();
Some of the fields in each table in dataset are primary keys and I dont want to show them in XML. If I exclude them from tables, I cant make relationships. Can anybody give me some idea that how to write dataset to XML "droping" the key fields (columns in datatables)? For example, here is the XML generating at the moment:
<?xml version="1.0"?>
<o>
<sp spname="SP1" spid="8">
<event spid="8" eventname="Event1" eventId="482">
<bm bmname="BM1" bmid="2" bmeid="826" eventid="482">
<att bmeid="826" val="3.00" attname="Att1" atttype="Type1" attid="23172"/>
<att bmeid="826" val="3.50" attname="Att2" bettype="Type1" attid="23173"/>
</bm>
</event>
</sp>
</o>
but I want this XML to be generated like this (all id attributes should be "dropped" as all ids are for relationships and should not be added in XML):
<?xml version="1.0"?>
<o>
<sp spname="SP1">
<event eventname="Event1">
<bm bmname="BM1" bmid="2">
<att val="3.00" attname="Att1" atttype="Type1" />
<att val="3.50" attname="Att2" bettype="Type1" />
</bm>
</event>
</sp>
</o>
And now 2nd question:
I've given name to my dataset as "o" so its generating the xml as you can see above. I want to add some attributes to <o>
node like current datetime. I mean I want <o>
node to be genrated as <o generatedDate="09/13/2011" generatedTime="03:45 PM">
. How can I achieve it?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以隐藏该列以将其从 XML 文件中排除:
You can hide the column to exclude it from the XML-File:
一种选择是使用 LINQ to XML 来更改文档。另一种选择是将 XML 传递到 XSLT 转换器,该转换器可以解析 XML 并输出所需的结果。
使用 XSLT 转换数据集:
至于以可读格式显示它,我建议将 XML 传递到 XSL 转换器,然后您可以使用它来解析 XML 并根据需要操作输出。
将 XSLT 转换应用于数据集
http://msdn.microsoft.com/en-us/library/8fd7xytc%28v=vs.71%29.aspx#Y289
这是我创建的一个简单示例,用于解释您如何使用使用 XSL 变压器。我还没有测试过它,但应该非常接近:
使用 XSLT 将 XML 格式化为 HTML
下面是使用 XSLT 将 XML 转换为 HTML 表的示例。它应该相当容易采用,因此您可以将它与序列化数据集一起使用。
假设这是您的数据集,序列化为 XML:
这是一个将数据集转换为 HTML 的 XSLT 脚本:
One option is to use LINQ to XML to alter the document. Another option would be to pass the XML into an XSLT transformer, which can parse the XML and output the desired results.
Transforming a DataSet using XSLT:
As for displaying it in a readable format, I would suggest passing the XML into an XSL transformer, which you can then use to parse the XML and manipulate the output as needed.
Applying an XSLT Transform to a DataSet
http://msdn.microsoft.com/en-us/library/8fd7xytc%28v=vs.71%29.aspx#Y289
Here's a simple example I created to explain how you would use the XSL transformer. I haven't tested it, but it should be pretty close:
Formatting XML as HTML using XSLT
Here's an example of using XSLT to transform XML into an HTML table. It should be fairly easy to adopt so you can use it with your serialized DataSet.
Let's say this is your DataSet, serialized to XML:
Here's an XSLT script that transforms the DataSet to HTML:
我建议在生成 XML 后,使用 LINQ to XML 来过滤/添加属性到所需的节点。此过滤和添加属性是一个单独的步骤,然后从数据集生成 XML,并且应该在从数据集生成 XML 之后进行处理,因为这将带来更好的设计。
I would suggest after generating the XML, use LINQ to XML to filter out / add attribute to desired nodes. This filtering and adding attribute is a separate step then generating the XML from data set and should be handled after the XML is generated from data set as that would lead to better design.