我想用dataset生成xml字符串,现在生成的字符串格式是:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table1>
    <dwid>13</dwid>
    <部门>73</部门>
    <职务>331</职务>
    <姓名>杨雪峰</姓名>
  </Table1>
</NewDataSet>
但是我需要的格式是:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table1 dwid="13" 部门="73" 职务="331" 姓名="杨雪峰" />
</NewDataSet>
应该怎么做呢?

解决方案 »

  1.   

    <dwid>13 </dwid> 
         <部门>73 </部门> 
         <职务>331 </职务> 
         <姓名>杨雪峰 </姓名> 
    这4个应该设置到element的属性里面
    using Microsoft.WindowsMediaServices.Interop;
    using interop_msxml;// Declare variables.
    WMSServer Server;
    IXMLDOMDocument Playlist;
    IXMLDOMNodeList ElemList;
    IXMLDOMElement Elem;
    IXMLDOMNode Node;try {
        // Create a new WMSServer object.
        Server = new WMSServerClass();    // Create a new playlist object.
        Playlist = Server.CreatePlaylist();    // Load a playlist.
        Playlist.load("file://c:\\wmpub\\wmroot\\simple.wsx");    // Retrieve a list of nodes that matches the query.
        ElemList = Playlist.getElementsByTagName("media");    // Retrieve the first node in the list.
        Node = ElemList[0];    // Box the first node into an IXMLDOMElement object.
        Elem = (IXMLDOMElement)Node;    // Set the value for the attribute named "src".
        Elem.setAttribute("src", "new_content.asf");//像这样的设置属性
    }
    catch (Exception e) {
        // TODO: Handle exceptions.
    }
      

  2.   

    XmlDocument doc = new XmlDocument();
                doc.LoadXml("<?xml version="1.0" standalone="yes"?> <NewDataSet>"+
      "<Table1dwid="13" 部门="73" 职务="331" 姓名="杨雪峰" /> </NewDataSet> ");
                doc.Save("d:aa.xml")
      

  3.   

    那我来个xslt处理的<?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
      <xsl:output method="xml" encoding="utf-8" indent="yes"/>
      <xsl:template match="/">
        <Data>
          <xsl:element name="Table1">
            <xsl:attribute name="dwid">
              <xsl:apply-templates  mode="attribute" select="//dwid"></xsl:apply-templates>
            </xsl:attribute>
              <xsl:attribute name="部门">
                <xsl:apply-templates  mode="attribute" select="//部门"></xsl:apply-templates>
              </xsl:attribute>
              <xsl:attribute name="职务">
                <xsl:apply-templates  mode="attribute" select="//职务"></xsl:apply-templates>
              </xsl:attribute>
              <xsl:attribute name="姓名">
                <xsl:apply-templates  mode="attribute" select="//姓名"></xsl:apply-templates>
              </xsl:attribute>
          </xsl:element>
        </Data>
      </xsl:template>
    </xsl:stylesheet>处理后的结果:<?xml version="1.0" encoding="utf-16"?>
    <Data>
      <Table1 dwid="13" 部门="73" 职务="331" 姓名="杨雪峰 " />
    </Data>建议使用xmldocument
      

  4.   

    参考http://www.cnblogs.com/ghd258/archive/2005/12/03/290034.html添加属性的部分
      

  5.   

     XmlAttribute xa=xmldoc.CreateAttribute("key");xa.Value="ltp";
      

  6.   

    我现在有一个dataset 里面table1有多行数据,能不能用 dataset.WriteXml直接生成我需要的格式的字符串
      

  7.   

    如果是sqlserver直接用sql语句生成
    select top 3 name,id from sysobjects
    for xml auto
    /*--查询结果
    <sysobjects name="sysrowsetcolumns" id="4"/>
    <sysobjects name="sysrowsets" id="5"/>
    <sysobjects name="sysallocunits" id="7"/>
    --*/
      

  8.   

    DataSet ds
    ds.GetXml();
    用这个方法系统帮你搞定呵呵
      

  9.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection())
                {
                    conn.ConnectionString = "server=.;uid=sa;pwd=***;database=master";
                    conn.Open();
                    System.Data.SqlClient.SqlCommand command = conn.CreateCommand();
                    command.CommandText = "select top 3 name,id from sysobjects for xml auto,root";
                    System.Xml.XmlReader reader= command.ExecuteXmlReader();
                    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                    doc.Load(reader);
                    Console.WriteLine(doc.OuterXml);
                    Console.Read();
                }        }
        }
    }