如何在一个很好的可读格式显示从数据表中WriteXML XML?很好、数据表、可读、格式

2023-09-04 04:31:07 作者:一定是你不努力学习

我有一些数据表,我要导出到一个文件,然后看看。我可以使用DataTable.WriteXml()输出的数据表的内容,以XML,但什么是一个简单的方法,以表格形式显示这些数据?

一个建议的方式是将使用XSLT或类似的到的东西可读的XML。我没有任何想法,如果这是简单的或没有,但说会是一个不错的解决方案,如果它很容易使用。

我从中WriteXML()为一个单一的数据表获得XML的一个例子是下面。这是一个名为RecentMatter'与10列和第3行的数据表。我想有它显示的如数据表。我不太关注的数据值,例如格式化转换日期时间值更可读价值。我生成此时所使用的参数XmlWriteMode.WriteSchema。如果删除然后所有架构信息(第一个标记之前)将被删除。

不同的表会显然有不同数量的列,因此任何解决方案将需要处理的,理想的,而不必修改与不同列的表的解决方案。

 < NewDataSet>
< XS:模式的xmlns =的xmlns:XS =htt​​p://www.w3.org/2001/XMLSchema的xmlns:MSDATA =瓮:架构 - 微软COM:XML-MSDATAID =NewDataSet >
< XS:元素的名称=NewDataSetMSDATA:IsDataSet =真MSDATA:MainDataTable =RecentMatterMSDATA:UseCurrentLocale =真正的>
< XS:复杂类型>
< XS:选择的minOccurs =0的maxOccurs =无界>
< XS:元素的名称=RecentMatter>
< XS:复杂类型>
< XS:序列>
< XS:元素的名称=用户登陆>
< XS:简单类型>
< XS:限制基地=XS:字符串>
< XS:最大长度值=2147483647/>
< / XS:限制>
< / XS:简单类型>
< / XS:组件>
< XS:元素的名称=MatterNumber>
< XS:简单类型>
< XS:限制基地=XS:字符串>
< XS:最大长度值=2147483647/>
< / XS:限制>
< / XS:简单类型>
< / XS:组件>
< XS:元素的名称=CLIENTNAME>
< XS:简单类型>
< XS:限制基地=XS:字符串>
< XS:最大长度值=2147483647/>
< / XS:限制>
< / XS:简单类型>
< / XS:组件>
< XS:元素的名称=MatterName>
< XS:简单类型>
< XS:限制基地=XS:字符串>
< XS:最大长度值=2147483647/>
< / XS:限制>
< / XS:简单类型>
< / XS:组件>
< XS:元素的名称=客户端code的minOccurs =0>
< XS:简单类型>
< XS:限制基地=XS:字符串>
< XS:最大长度值=2147483647/>
< / XS:限制>
< / XS:简单类型>
< / XS:组件>
< XS:元素的名称=办公室code的minOccurs =0>
< XS:简单类型>
< XS:限制基地=XS:字符串>
< XS:最大长度值=2147483647/>
< / XS:限制>
< / XS:简单类型>
< / XS:组件>
< XS:元素的名称=OfficeName的minOccurs =0>
< XS:简单类型>
< XS:限制基地=XS:字符串>
< XS:最大长度值=2147483647/>
< / XS:限制>
< / XS:简单类型>
< / XS:组件>
< XS:元素的名称=计费类型=XS:布尔/>
< XS:元素的名称=ReferenceId的minOccurs =0>
< XS:简单类型>
< XS:限制基地=XS:字符串>
< XS:最大长度值=2147483647/>
< / XS:限制>
< / XS:简单类型>
< / XS:组件>
< XS:元素的名称=LASTUSED类型=XS:日期时间/>
< / XS:序列>
< / XS:复杂类型>
< / XS:组件>
< / XS:选择>
< / XS:复杂类型>
< XS:唯一的名称=Constraint1MSDATA:的PrimaryKey =真正的>
< XS:选择的XPath =// RecentMatter/>
< XS:字段的XPath =用户登陆/>
< XS:字段的XPath =MatterNumber/>
< / XS:唯一>
< / XS:组件>
< / XS:模式>

< RecentMatter>
  <用户登陆> PSLTP6 \ RJK< /用户登陆>
  < MatterNumber> 99999-2302< / MatterNumber>
  < CLIENTNAME>测试事项和LT; / CLIENTNAME>
  < MatterName> DP测试物质和LT; / MatterName>
  <客户code> 99999< /客户端code取代;
  <办公code />
  < OfficeName />
  <计费>真< /计费>
  < ReferenceId />
  < LASTUSED> 2011-08-23T23:40:24.13 + 01:00< / LASTUSED>
< / RecentMatter>
< RecentMatter>
  <用户登陆> PSLTP6 \ RJK< /用户登陆>
  < MatterNumber> 999991.0002< / MatterNumber>
  < CLIENTNAME>车床1< / CLIENTNAME>
  < MatterName> LW测试2版; / MatterName>
  <客户code />
  <办公code />
  < OfficeName />
  <计费>真< /计费>
  < ReferenceId />
  < LASTUSED> 2011-07-12T16:57:27.173 + 01:00< / LASTUSED>
< / RecentMatter>
< RecentMatter>
  <用户登陆> PSLTP6 \ RJK< /用户登陆>
  < MatterNumber> 999991-0001< / MatterNumber>
  < CLIENTNAME>车床1< / CLIENTNAME>
  < MatterName> LW测试1< / MatterName>
  <客户code />
  <办公code />
  < OfficeName />
  <计费>假< /计费>
  < ReferenceId />
  < LASTUSED> 2011-07-12T01:59:06.887 + 01:00< / LASTUSED>
< / RecentMatter>
< / NewDataSet>
 

解决方案

给这个一杆。也许最简单的方法来一个DataTable序列化到XML:

 数据表表=新的DataTable();
System.IO.StringWriter作家=新System.IO.StringWriter();

//注意我们忽略的模式,所以我们得到干净的XML回来
//为需要得到你的结果,你可以更改写模式
table.WriteXml(作家,XmlWriteMode.IgnoreSchema,假);

字符串dataTableXml = writer.ToString();
 

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

应用XSLT转换到DataSet

XML 阅读器

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

下面是我创建的解释你将如何使用XSL转换一个简单的例子。我没有测试它,但它应该pretty的接近:

 的DataSet ds为新的DataSet();
StringBuilder的sbXslOutput =新的StringBuilder();

使用(XmlWriter的xslWriter = XmlWriter.Create(sbXslOutput))
{
    XslCompiledTransform变压器=新XslCompiledTransform();
    transformer.Load(transformer.xsl);
    的XsltArgumentList的args =新的XsltArgumentList();

    transformer.Transform(新的XmlDataDocument(DS),指定参数时,xslWriter);
}

字符串dataSetHtml = sbXslOutput.ToString();
 

使用XSLT格式化XML作为HTML

下面是一个使用XSLT将XML转换为HTML表的一个例子。它应该很容易通过,所以你可以使用它与你的序列化的DataSet。

 < XSL:样式的xmlns:XSL =htt​​p://www.w3.org/1999/XSL/Transform版本=1.0>
  < XSL:模板匹配=/>
      <表格边框=1>
        &其中; TR>
          百分位>用户登录< /第i个
          <第i个物质数量和LT; /第i个
          ...
        < / TR>
        <的xsl:for-每个选择=NewDataSet / RecentMatter>
          &其中; TR>
            < TD>
              < XSL:选择=用户登陆/&GT价值的;
            < / TD>
            < TD>
              < XSL:选择=MatterNumber/&GT价值的;
            < / TD>
            ...
          < / TR>
        < /的xsl:for-每个>
      < /表>
  < / XSL:模板>
< / XSL:样式>
 

I have some DataTables that I want to export to a file and then look at. I can use DataTable.WriteXml() to output the contents of the DataTables to XML but what's an easy way to display this data in tabular form?

One suggested way is to transform the XML using XSLT or similar into something readable. I don't have any idea if that's easy or not but that'd be a fine solution if it's easy to use.

An example of the XML I get from WriteXml() for a single DataTable is below. This is for a table named 'RecentMatter' with 10 columns and 3 rows of data. I'd like to have it displayed like a table of data. I'm less concerned about formatting of data values, e.g. converting DateTime values to more readable values. I used the XmlWriteMode.WriteSchema parameter when generating this. If you remove that then all the schema info (before the first tag) is removed.

Different tables will obviously have different numbers of columns, so any solution would need to deal with that, ideally without having to modify the solution for tables with different columns.

<NewDataSet>
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="RecentMatter" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="RecentMatter">
<xs:complexType>
<xs:sequence>
<xs:element name="UserLogin">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2147483647"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="MatterNumber">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2147483647"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="ClientName">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2147483647"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="MatterName">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2147483647"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="ClientCode" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2147483647"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="OfficeCode" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2147483647"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="OfficeName" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2147483647"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Billable" type="xs:boolean"/>
<xs:element name="ReferenceId" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2147483647"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="LastUsed" type="xs:dateTime"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//RecentMatter"/>
<xs:field xpath="UserLogin"/>
<xs:field xpath="MatterNumber"/>
</xs:unique>
</xs:element>
</xs:schema>

<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>

解决方案

Give this a shot. Probably the easiest way to serialize a DataTable to XML:

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.

<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>