读到dataset里,然后dataset.WriteXml()

解决方案 »

  1.   

    the xml format will be like this, how does your database table look like?<MenuData> 
      <MenuGroup> 
        <MenuItem Label="Products" >
        <MenuGroup>
          <MenuItem Label="Web Controls" />
          <MenuItem Label="Win Controls" />
          <MenuItem Label="Other Tools" />
        </MenuGroup>
        </MenuItem>    <MenuItem Label="Services">
        <MenuGroup>
          <MenuItem Label="Consulting" />
          <MenuItem Label="Web Production" />
          <MenuItem Label="System Integration" />
        </MenuGroup>
        </MenuItem>
      </MenuGroup>
    </MenuData> 
      

  2.   

    dataset.WriteXml()读出数据,在通过xsl来进行相应转换达到需要的格式要求!
      

  3.   

    <MenuData> 
      <MenuGroup>
      <MenuItem Label="Admin" >
          <MenuGroup ID="g_Admin">
            <MenuItem Label="Employees" URL="../MenuNav.aspx?sec=emp" />
            <MenuItem Label="Users" URL="../MenuNav.aspx?sec=users"/>
            <MenuItem Label="GP" URL="../MenuNav.aspx?sec=gp"/>
            <MenuItem Label="Mother Transfer " URL="../MenuNav.aspx?sec=tran"/>
            <MenuItem Label="Reports" URL="../MenuNav.aspx?sec=rep"/>
           </MenuGroup>
        </MenuItem>
        
        <MenuItem Label="System">
          <MenuGroup ID="g_System">
          <MenuItem Label="Back to First Page" URL="../MenuNav.aspx?sec=first"  />
            <MenuItem Label="Change Password" URL="../MenuNav.aspx?sec=pwd" />
            <MenuItem Label="Relogin" URL="../MenuNav.aspx?sec=login"/>
            <MenuItem Label="Exit System" URL="../MenuNav.aspx?sec=exit"/>
          </MenuGroup>
        </MenuItem>
        
        <MenuItem Label="Mims" URL="Top.aspx?section=mims">
          <MenuGroup ID="g_Mims">
            <MenuItem Label="Search" URL="Top.aspx?section=search"/>
            <MenuItem Label="Therapeutic Class" URL="Top.aspx?section=the"/>
            <MenuItem Label="Alphabet" URL="Top.aspx?section=alp"/>
          </MenuGroup>
        </MenuItem>    <MenuItem Label="ICD10" URL="Top.aspx?section=icd10" >   
       <MenuGroup ID="g_ICD10">
            <MenuItem Label="Diseases" URL="Top.aspx?section=dis"/>
            <MenuItem Label="Procedures" URL="Top.aspx?section=pro"/>
          </MenuGroup>
        </MenuItem>
        <MenuItem Label="Tools" URL="Top.aspx?section=tools" >   
    <MenuGroup ID="g_tools">
             <MenuItem Label="Tools1" URL="Top.aspx?section=t1"/>
             </MenuGroup>
        </MenuItem>
        <MenuItem Label="Help" >
    <MenuGroup ID="g_Help">
            <MenuItem Label="Index" URL="Top.aspx?section=index"/>
            <MenuItem Label="About..." URL="Top.aspx?section=about"/>
          </MenuGroup>
        </MenuItem>
      </MenuGroup>
    </MenuData> 
      

  4.   

    to: saucer(思归/MVP) 大哥
    我的表字段是:ID,FID,Name 分别是菜单ID,父ID,菜单名称
    直接从数据库中生成xml文件如下:
    <newdataset>
      <table>
         <ID>1000</ID>
         <FID>-1</FID>
         <Name>File</Name>
      </table>
      <table>
         <ID>1001</ID>
         <FID>1000</FID>
         <Name>New</Name>
      </table>
      ...
    </newdataset>
      
    请问怎样可格式化成如下格式呢?
    xslt初学,请指教!<MenuData> 
      <MenuGroup> 
        <MenuItem Label="Products" >
        <MenuGroup>
          <MenuItem Label="Web Controls" />
          <MenuItem Label="Win Controls" />
          <MenuItem Label="Other Tools" />
        </MenuGroup>
        </MenuItem>    <MenuItem Label="Services">
        <MenuGroup>
          <MenuItem Label="Consulting" />
          <MenuItem Label="Web Production" />
          <MenuItem Label="System Integration" />
        </MenuGroup>
        </MenuItem>
      </MenuGroup>
    </MenuData
      

  5.   

    本文通过实际的例子来说明如何在XSL中实现对XML数据转换成完美的多列表格。在利用XSL对XML进行转换时,有时候需要把XML转换成多列的Table元素,这个问题经常会困扰许多人,如果不生成Table的话,只需要对循环中的节点进行位置取模后判断,然后用<br/>换行即可。但有时候为了用户需要和界面的美观,需要生成多行多列的Table,常用的方法是采用following-sibling进行判断,比如下面的代码: <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:copyRight="http://xml.sz.luohuedu.net/">
      <xsl:template match="/">
      <table bgcolor="snow" border="1" cellpadding="5" cellspacing="2" borderColor="darkorange"
       style="font-size:9pt">
      <xsl:for-each select="/Items/Item[position() mod 3 = 1]">
        <tr>
          <td width="33%" align="center" valign="middle">
            <xsl:apply-templates select="."/>
          </td>
          <td width="34%" align="center" valign="middle">
            <xsl:apply-templates select="following-sibling::Item[position() = 1]"/>
          </td>
          <td width="33%" align="center" valign="middle">
            <xsl:apply-templates select="following-sibling::Item[position() = 2]"/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
      </xsl:template>
      <xsl:template match="/Items/Item">
        <a target="_blank">
          <xsl:attribute name="href">
            <xsl:if test="contains(Url,'@')">mailto:</xsl:if><xsl:value-of select="Url"/></xsl:attribute>
          <xsl:value-of select="Title"/>
        </a>
      </xsl:template>
    </xsl:stylesheet>
      

  6.   

    理解了上面的原理之后,下面就是我们最后的代码,程序已经做了注释: <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:copyRight="http://xml.sz.luohuedu.net/">
      <xsl:template match="/">
       <!-- 定义常量 -->
        <xsl:variable name="strTrLeft" select="'&lt;tr&gt;'"/>
        <xsl:variable name="strTrRight" select="'&lt;/tr&gt;'"/>
        <!-- 计算总记录数 -->
        <xsl:variable name="nTotal" select="count(/Items/Item)"/>
        <!-- 定义列数 -->
        <xsl:variable name="nCols" select="3"/>
        <!-- 计算需要的补齐的列数 -->
        <xsl:variable name="nLefted" select="$nCols - ($nTotal mod $nCols)"/>
        <!-- 计算不需要补齐的行数 -->
        <xsl:variable name="nNotProcessedRow" select="$nTotal -  ($nTotal mod $nCols)"/>
        <table bgcolor="snow" border="1" cellpadding="5" cellspacing="2" borderColor="darkorange"
          style="font-size:9pt">
         <!-- 对于不需要补齐的行数,直接输出 -->
          <xsl:for-each select="/Items/Item[position() &lt; $nNotProcessedRow +1]">
            <xsl:if test="position() mod $nCols = 1">
              <xsl:value-of select="$strTrLeft" disable-output-escaping="yes"/>
            </xsl:if>
            <td>
        <a target="_blank">
          <xsl:attribute name="href">
            <xsl:if test="contains(Url,'@')">mailto:</xsl:if><xsl:value-of select="Url"/></xsl:attribute>
          <xsl:value-of select="Title"/>
        </a>
            </td>
            <xsl:if test="position() mod $nCols = 0">
              <xsl:value-of select="$strTrRight" disable-output-escaping="yes"/>
            </xsl:if>
          </xsl:for-each>
          <!-- 转换除去不需要补齐的记录的剩余记录 -->
          <xsl:if test="$nLefted != 0 and $nLefted != $nCols">
            <xsl:value-of select="$strTrLeft" disable-output-escaping="yes"/>
            <xsl:for-each select="/Items/Item[position() &gt;$nNotProcessedRow]">
              <td>
        <a target="_blank">
          <xsl:attribute name="href">
            <xsl:if test="contains(Url,'@')">mailto:</xsl:if><xsl:value-of select="Url"/></xsl:attribute>
          <xsl:value-of select="Title"/>
        </a>
              </td>
            </xsl:for-each>
            <!--
            如果nLefted不等于0和列数,则需要进行补齐,这里进行递归调用,需要传递的参数有两个:
            nLefted:要补齐的列数;
            nCols:表格的列数。
            -->
            <xsl:call-template name="MyFun">
              <xsl:with-param name="nLefted" select="$nLefted"/>
              <xsl:with-param name="nCols" select="$nCols"/>
            </xsl:call-template>
            <xsl:value-of select="$strTrRight" disable-output-escaping="yes"/>
          </xsl:if>
        </table>
        <p>共有<xsl:value-of select="$nTotal"/>条数据。</p>
      </xsl:template>
      <xsl:template name="MyFun">
        <xsl:param name="nLefted"/>
        <xsl:param name="nCols"/>
        <xsl:if test=" $nLefted != 0 and $nLefted != $nCols">
          <td>
            <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
          </td>
          <xsl:call-template name="MyFun">
            <xsl:with-param name="nLefted" select="$nLefted - 1"/>
            <xsl:with-param name="nCols" select="$nCols"/>
          </xsl:call-template>
        </xsl:if>
      </xsl:template>
    </xsl:stylesheet>由于http://www.w3.org/1999/XSL/Transform名称控件只有IE5.5+才支持,为了使我们的代码具有通用性,我们在服务器端进行转换,首先建立GoodLoop.aspx如下: <%@ Page Language="vb" AutoEventWireup="false" Codebehind="GoodLoop.aspx.vb"
     Inherits="aspxWeb.mengxianhui.com.GoodLoop"%>
    <%@ Import NameSpace = "System" %>
    <%@ Import NameSpace = "System.Xml" %>
    <%@ Import NameSpace = "system.Xml.Xsl" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
      <HEAD>
        <title>GoodLoop</title>
        <meta content="Microsoft Visual Studio .NET 7.0" name="GENERATOR">
        <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
        <script runat=Server>
      Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
          Dim Xmldom As New XmlDocument()
          Xmldom.Load(Server.MapPath("GoodLoop.xml"))
          Dim trans As XslTransform = New XslTransform()
          trans.Load(Server.MapPath("GoodLoop.xsl"))
          Xml1.Document = Xmldom
          Xml1.Transform = trans
        Catch er As XmlException
          Label1.Text = er.Message
        End Try
      End Sub
        </script>
      </HEAD>
      <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
          <asp:label id="Label1" runat="server"></asp:label>
          <asp:Xml id="Xml1" runat="server"></asp:Xml></form>
      </body>
    </HTML>本文中所使用的XML数据样式为:GoodLoop.xml<?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="GoodLoop1.xsl"?>
    <Items>
      <Item>
        <Url>http://xml.sz.luohuedu.net</Url>
        <Title>【孟宪会之精彩世界】</Title>
      </Item>
      <Item>
        <Url>http://lucky.myrice.com/</Url>
        <Title>【孟宪会之精彩世界】</Title>
      </Item>
    ...............................
    </Items>