DataSet.WriteXml() - 如何“删除”一些领域

发布于 2024-12-04 19:53:50 字数 1520 浏览 1 评论 0原文

我有 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 技术交流群。

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

发布评论

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

评论(3

苍景流年 2024-12-11 19:53:50

您可以隐藏该列以将其从 XML 文件中排除:

dataSet.Tables["TableName"].Columns["ColumnName"].ColumnMapping = MappingType.Hidden;

You can hide the column to exclude it from the XML-File:

dataSet.Tables["TableName"].Columns["ColumnName"].ColumnMapping = MappingType.Hidden;
蔚蓝源自深海 2024-12-11 19:53:50

一种选择是使用 LINQ to XML 来更改文档。另一种选择是将 XML 传递到 XSLT 转换器,该转换器可以解析 XML 并输出所需的结果。

使用 XSLT 转换数据集:

DataTable table = new DataTable();    
System.IO.StringWriter writer = new System.IO.StringWriter();

//notice that we're ignoring the schema so we get clean XML back
//you can change the write mode as needed to get your result
table.WriteXml(writer, XmlWriteMode.IgnoreSchema, false);

string dataTableXml = writer.ToString();

至于以可读格式显示它,我建议将 XML 传递到 XSL 转换器,然后您可以使用它来解析 XML 并根据需要操作输出。


将 XSLT 转换应用于数据集

http://msdn.microsoft.com/en-us/library/8fd7xytc%28v=vs.71%29.aspx#Y289

这是我创建的一个简单示例,用于解释您如何使用使用 XSL 变压器。我还没有测试过它,但应该非常接近:

DataSet ds = new DataSet();
StringBuilder sbXslOutput = new StringBuilder();

using (XmlWriter xslWriter = XmlWriter.Create(sbXslOutput))
{
    XslCompiledTransform transformer = new XslCompiledTransform();
    transformer.Load("transformer.xsl");
    XsltArgumentList args = new XsltArgumentList();

    transformer.Transform(new XmlDataDocument(ds), args, xslWriter);
}

string dataSetHtml = sbXslOutput.ToString();

使用 XSLT 将 XML 格式化为 HTML

下面是使用 XSLT 将 XML 转换为 HTML 表的示例。它应该相当容易采用,因此您可以将它与序列化数据集一起使用。

假设这是您的数据集,序列化为 XML:

<RecentMatter> 
  <UserLogin>PSLTP6\RJK</UserLogin> 
  <MatterNumber>99999-2302</MatterNumber> 
  <ClientName>Test Matters</ClientName> 
  <MatterName>DP Test Matter</MatterName> 
  <ClientCode>99999</ClientCode> 
  <OfficeCode/> 
  <OfficeName/> 
  <Billable>true</Billable> 
  <ReferenceId/> 
  <LastUsed>2011-08-23T23:40:24.13+01:00</LastUsed> 
</RecentMatter> 
<RecentMatter> 
  <UserLogin>PSLTP6\RJK</UserLogin> 
  <MatterNumber>999991.0002</MatterNumber> 
  <ClientName>Lathe 1</ClientName> 
  <MatterName>LW Test 2</MatterName> 
  <ClientCode/> 
  <OfficeCode/> 
  <OfficeName/> 
  <Billable>true</Billable> 
  <ReferenceId/> 
  <LastUsed>2011-07-12T16:57:27.173+01:00</LastUsed> 
</RecentMatter> 
<RecentMatter> 
  <UserLogin>PSLTP6\RJK</UserLogin> 
  <MatterNumber>999991-0001</MatterNumber> 
  <ClientName>Lathe 1</ClientName> 
  <MatterName>LW Test 1</MatterName> 
  <ClientCode/> 
  <OfficeCode/> 
  <OfficeName/> 
  <Billable>false</Billable> 
  <ReferenceId/> 
  <LastUsed>2011-07-12T01:59:06.887+01:00</LastUsed> 
</RecentMatter> 
</NewDataSet> 

这是一个将数据集转换为 HTML 的 XSLT 脚本:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
      <table border="1">
        <tr>
          <th>User Login</th>
          <th>Matter Number</th>
          ...
        </tr>
        <xsl:for-each select="NewDataSet/RecentMatter">
          <tr>
            <td>
              <xsl:value-of select="UserLogin"/>
            </td>
            <td>
              <xsl:value-of select="MatterNumber"/>
            </td>
            ...
          </tr>
        </xsl:for-each>
      </table>
  </xsl:template>
</xsl:stylesheet>

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:

DataTable table = new DataTable();    
System.IO.StringWriter writer = new System.IO.StringWriter();

//notice that we're ignoring the schema so we get clean XML back
//you can change the write mode as needed to get your result
table.WriteXml(writer, XmlWriteMode.IgnoreSchema, false);

string dataTableXml = writer.ToString();

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:

DataSet ds = new DataSet();
StringBuilder sbXslOutput = new StringBuilder();

using (XmlWriter xslWriter = XmlWriter.Create(sbXslOutput))
{
    XslCompiledTransform transformer = new XslCompiledTransform();
    transformer.Load("transformer.xsl");
    XsltArgumentList args = new XsltArgumentList();

    transformer.Transform(new XmlDataDocument(ds), args, xslWriter);
}

string dataSetHtml = sbXslOutput.ToString();

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:

<RecentMatter> 
  <UserLogin>PSLTP6\RJK</UserLogin> 
  <MatterNumber>99999-2302</MatterNumber> 
  <ClientName>Test Matters</ClientName> 
  <MatterName>DP Test Matter</MatterName> 
  <ClientCode>99999</ClientCode> 
  <OfficeCode/> 
  <OfficeName/> 
  <Billable>true</Billable> 
  <ReferenceId/> 
  <LastUsed>2011-08-23T23:40:24.13+01:00</LastUsed> 
</RecentMatter> 
<RecentMatter> 
  <UserLogin>PSLTP6\RJK</UserLogin> 
  <MatterNumber>999991.0002</MatterNumber> 
  <ClientName>Lathe 1</ClientName> 
  <MatterName>LW Test 2</MatterName> 
  <ClientCode/> 
  <OfficeCode/> 
  <OfficeName/> 
  <Billable>true</Billable> 
  <ReferenceId/> 
  <LastUsed>2011-07-12T16:57:27.173+01:00</LastUsed> 
</RecentMatter> 
<RecentMatter> 
  <UserLogin>PSLTP6\RJK</UserLogin> 
  <MatterNumber>999991-0001</MatterNumber> 
  <ClientName>Lathe 1</ClientName> 
  <MatterName>LW Test 1</MatterName> 
  <ClientCode/> 
  <OfficeCode/> 
  <OfficeName/> 
  <Billable>false</Billable> 
  <ReferenceId/> 
  <LastUsed>2011-07-12T01:59:06.887+01:00</LastUsed> 
</RecentMatter> 
</NewDataSet> 

Here's an XSLT script that transforms the DataSet to HTML:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
      <table border="1">
        <tr>
          <th>User Login</th>
          <th>Matter Number</th>
          ...
        </tr>
        <xsl:for-each select="NewDataSet/RecentMatter">
          <tr>
            <td>
              <xsl:value-of select="UserLogin"/>
            </td>
            <td>
              <xsl:value-of select="MatterNumber"/>
            </td>
            ...
          </tr>
        </xsl:for-each>
      </table>
  </xsl:template>
</xsl:stylesheet>
感悟人生的甜 2024-12-11 19:53:50

我建议在生成 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.

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