<aa>
    <cc value="00001" name="code"/>
    <cc value="admin3" name="name"/>
  <cc value="192.168.1.100" name="IP"/>
  <cc value="使用测试" name="re"/>
</aa>
根据数据库中表的结构生成类似于上面的xml文件,name为表中字段,value为字段对应值。请大家帮忙,谢谢!

解决方案 »

  1.   

    自己创建一个XML根节点如数据库中字段对应再用个foreach循环。。在循环中创建字节点添加在根节点上
      

  2.   

    感觉.net中操作xml。也像dom一样。
      

  3.   

    哪位能不能提供代码,DataSet.WriteXml()生成的都是在一个节点里,不会分开出来。
      

  4.   

    假设你从数据库中读出的数据保存在一个DataTable dt中,XmlDocument xd = new XmlDocument();
    XmlDeclaration xmldecl = xd.CreateXmlDeclaration("1.0", "utf-8", "yes");
    //写入根结点
    XmlElement aa = xd.CreateElement("aa");
    xd.AppendChild(dd);
    foreach (DataRow r in dt.Rows)
    {
        XmlElement cc = xd.CreateElement("cc");
        cc.SetAttribute("value", r["value"].ToString());
        cc.SetAttribute("name", r["name"].ToString());
        aa.AppendChild(cc);
    }
    xd.Save(Server.MapPath("~/文件名.xml"));
      

  5.   

    写错了一个词XmlDocument xd = new XmlDocument();
    XmlDeclaration xmldecl = xd.CreateXmlDeclaration("1.0", "utf-8", "yes");
    //写入根结点
    XmlElement aa = xd.CreateElement("aa");
    xd.AppendChild(aa);
    foreach (DataRow r in dt.Rows)
    {
        XmlElement cc = xd.CreateElement("cc");
        cc.SetAttribute("value", r["value"].ToString());
        cc.SetAttribute("name", r["name"].ToString());
        aa.AppendChild(cc);
    }
    xd.Save(Server.MapPath("~/文件名.xml"));