比如我有一张数据库表table1,如后这张表里面有如下数据:
字段1字段2字段3
A   1001   abc  
A   1002   abd
A   1003   deg
B   2001   der
B   2002   der
C   3001   der
D   4005   der
D   4002   swe
从数据库里面获取所有数据,以字段1来区分,生成 四个xml文件
也就是说:
A   1001   abc  
A   1002   abd
A   1003   deg
这个放在以a.xml文件里面
B   2001   der
B   2002   der
这个放在b.xml文件里面以此类推,请给位大侠赐教,谢谢!

解决方案 »

  1.   

    给你创建xml的代码,自己填充吧                    XmlDocument userdoc = new XmlDocument();
                        userdoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><UserStockGroup></UserStockGroup>");
                        XmlElement newGroup = userdoc.CreateElement("UserStockGroupItem");
                        newGroup.SetAttribute("name", "自选股");
                        newGroup.SetAttribute("id", "0");
                        userdoc.DocumentElement.AppendChild(newGroup);
                        userdoc.Save("c:\\1.xml");
      

  2.   

    这个好像没有什么难度吧关键是你得XML格式如何定义使用C#自带的XML操作类就可以实现
      

  3.   

    如果只是查询出来直接生成,结果集可以直接 to XML如果有特殊要求就只能自己单写了。
    如:
     XmlNode root = xmlDoc.SelectSingleNode("/RoleDefautPage/Roles");
                    XmlElement xe = xmlDoc.CreateElement("Role");//创建一个<Role>节点
                    xe.SetAttribute("ID", id);//设置节点 属性
                    xe.SetAttribute("Name", role);//设置节点 属性
                    xe.SetAttribute("Status", "0");//设置节点 属性
                    root.AppendChild(xe);
                    xmlDoc.Save(file);
      

  4.   

    如要完整示例可以去51aspx下一个
      

  5.   

    代码        static void Main(string[] args)
            {
                string srcData = @"A 1001 abc   
    A 1002 abd
    A 1003 deg
    B 2001 der
    B 2002 der
    C 3001 der
    D 4005 der
    D 4002 swe";            Dictionary<string, XmlDocument> xmlDocs = new Dictionary<string, XmlDocument>();            StringReader reader = new StringReader(srcData);
                string line = reader.ReadLine();
                while (line != null)
                {
                    string[] parts = line.Split();
                    XmlDocument xmlDoc = null;
                    if (xmlDocs.ContainsKey(parts[0]))
                        xmlDoc = xmlDocs[parts[0]];
                    else
                    {
                        xmlDoc = new XmlDocument();
                        xmlDoc.LoadXml("<Root></Root>");
                        xmlDocs.Add(parts[0], xmlDoc);
                    }
                    XmlElement ele = xmlDoc.CreateElement("Item");
                    ele.SetAttribute("Column1", parts[0]);
                    ele.SetAttribute("Column2", parts[1]);
                    ele.SetAttribute("Column3", parts[2]);
                    xmlDoc.DocumentElement.AppendChild(ele);
                    line = reader.ReadLine();
                }            foreach (KeyValuePair<string, XmlDocument> pair in xmlDocs)
                {
                    pair.Value.Save("D:\\" + pair.Key + ".xml");
                }
            }文件内容A.xml:<Root>
      <Item Column1="A" Column2="1001" Column3="abc" />
      <Item Column1="A" Column2="1002" Column3="abd" />
      <Item Column1="A" Column2="1003" Column3="deg" />
    </Root>B.xml<Root>
      <Item Column1="B" Column2="2001" Column3="der" />
      <Item Column1="B" Column2="2002" Column3="der" />
    </Root>...
      

  6.   

     XDocument Document = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
                    new XElement("root",
                        new XAttribute("imageWidth","440"),
                        new XAttribute("imageHeight","250")));
                foreach (var v in Enumerable.Range(1, 10))
                {
                    Document.Element("root").Add(new XElement("menu",
                        new XAttribute("url", String.Format("http://localhost:9671/Web/WorksDetail.aspx?id={0}",v)),
                        new XAttribute("frame", "_blank"),
                        new XAttribute("imageUrl", String.Format("images/{0}.jpg",v))));            }
                Document.Save("c:\\txt.xml");
      

  7.   

    class MainClass
        {
            XmlDocument xmldoc;
            XmlNode xmlnode;
            XmlElement xmlelem;
            XmlElement xmlelem2;
            XmlText xmltext;        public SaveXml()
            {            xmldoc = new XmlDocument();
                //加入XML的声明段落
                xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                xmldoc.AppendChild(xmlnode);
                //加入一个根元素
                xmlelem = xmldoc.CreateElement("", "ROOT", "");
                xmltext = xmldoc.CreateTextNode("Root Text");
                xmlelem.AppendChild(xmltext);
                xmldoc.AppendChild(xmlelem);
                //加入另外一个元素
                xmlelem2 = xmldoc.CreateElement("SampleElement");
                xmlelem2 = xmldoc.CreateElement("", "SampleElement", "");
                xmltext = xmldoc.CreateTextNode("The text of the sample element");
                xmlelem2.AppendChild(xmltext);
                xmldoc.ChildNodes.Item(1).AppendChild(xmlelem2);
                //保存创建好的XML文档
                try
                {
                    xmldoc.Save("d:\\data1.xml");
                }
                catch (Exception e)
                {
                    //显示错误信息
                    Console.WriteLine(e.Message);
                }
                //Console.ReadLine ( ) ;
            }    }}
      

  8.   

    A.xml:<Root>
      <root1 name="df">
      <Item Column1="A" Column2="1001" Column3="abc" />
      <Item Column1="A" Column2="1002" Column3="abd" />
      <Item Column1="A" Column2="1003" Column3="deg" />
      <root1>
    </Root>我想在里面再多加几个元素  比如让面的root1该怎么加进去啊
      

  9.   


                while (line != null)
                {
                    string[] parts = line.Split();
                    XmlDocument xmlDoc = null;
                    if (xmlDocs.ContainsKey(parts[0]))
                        xmlDoc = xmlDocs[parts[0]];
                    else
                    {
                        xmlDoc = new XmlDocument();
                        xmlDoc.LoadXml("<Root></Root>");
                        XmlElement eleRoot1 = xmlDoc.CreateElement("root1");
                        eleRoot1.SetAttribute("name", "df");
                        xmlDoc.DocumentElement.AppendChild(eleRoot1);
                        xmlDocs.Add(parts[0], xmlDoc);
                    }
                    XmlElement ele = xmlDoc.CreateElement("Item");
                    ele.SetAttribute("Column1", parts[0]);
                    ele.SetAttribute("Column2", parts[1]);
                    ele.SetAttribute("Column3", parts[2]);
                    xmlDoc.DocumentElement["root1"].AppendChild(ele);
                    line = reader.ReadLine();
                }
      

  10.   

    属性名中间不能有空格?ele.SetAttribute("Column Name", parts[0]);这样就不行
      

  11.   

    当然不能有空格啊,你想想
     <Item Column Name="1001" />
    这都不符合XML的格式
      

  12.   

    给根目录root里面插入数据  要怎么做啊?
      

  13.   

    http://www.cnblogs.com/hulang/archive/2011/01/11/1932568.html