看了很多文章,但是还是不太清楚,所以来求教一下!
情况是这样的:我现在用C#连数据库,将一张表里的数据读了出来,通过程序将数据导出,生成固定格式的XML文档。
XML的格式是这样的:<?xml version="1.0" encoding="GB2312"?>
<FileId fileid="1">
<ResultSet>
<row id="0">
<col name="KHID">24</col>     
<col name="LXR">&Iacute;&otilde;&cedil;&Otilde;</col>
<col name="FWDF">0</col>
<col name="YJJY">SDFADSAFDSAFDSAFDSAFDSAFDSAFDSAFDSAFDSAtest1</col>
<col name="HFRID">26</col>
<col name="HFRQ">2004-09-21</col>
<col name="KHWT">SDAFDSA</col>
<col name="CLJG">FDSAFDSAFDS</col>
<col name="ZZDJH"></col>
<col name="LRRID">26</col>
<col name="CJRQ">2004-09-21</col>
</row>
<row id="1">
<col name="KHID">25</col>
<col name="LXR">&Otilde;&Aring;&Egrave;&yacute;</col>
<col name="FWDF">0</col>
<col name="YJJY">SDFDSAFDSAFSAFDSAFDSAFDSAtest2</col>
<col name="HFRID">26</col>
<col name="HFRQ">2004-09-21</col>
<col name="KHWT">fsgdgfdsgfds</col>
<col name="CLJG">fdsgfdsgfds</col>
<col name="ZZDJH"></col>
<col name="LRRID">26</col>
<col name="CJRQ">2004-09-21</col>
</row>
</ResultSet>
</FileId>
=========================================================
我自己到网上找了些代码,试了一下,但都不成功。一开始,我是想用一个循环去一条条的写,但是
在 XmlElement xesub2=xmlDoc.CreateElement(col name="LXR">);的时候报错,不能有空格。不知道怎么处理了!
还有我在网上看到可以直接从 ds.WriteXml("myxml.xml", XmlWriteMode.WriteSchema);来生成XML文件,但是出来的格式不正确,因该如何做啊?谢谢大家帮帮忙! 在线等

解决方案 »

  1.   

    #region Overload Method public int Read(string section, string key, int defaultValue)
    {
    object o = Read(section, key);
    return o == null ? defaultValue : Convert.ToInt32(o);
    } public long Read(string section, string key, long defaultValue)
    {
    object o = Read(section, key);
    return o == null ? defaultValue : Convert.ToInt64(o);
    } public string Read(string section, string key, string defaultValue)
    {
    object o = Read(section, key);
    return o == null ? defaultValue : Convert.ToString(o);
    } public bool Read(string section, string key, bool defaultValue)
    {
    object o = Read(section, key);
    return o == null ? defaultValue : Convert.ToBoolean(o);
    } public float Read(string section, string key, float defaultValue)
    {
    object o = Read(section, key);
    return o == null ? defaultValue : Convert.ToSingle(o);
    } public double Read(string section, string key, double defaultValue)
    {
    object o = Read(section, key);
    return o == null ? defaultValue : Convert.ToDouble(o);
    } public decimal Read(string section, string key, decimal defaultValue)
    {
    object o = Read(section, key);
    return o == null ? defaultValue : Convert.ToDecimal(o);
    } public DateTime Read(string section, string key, DateTime defaultValue)
    {
    object o = Read(section, key);
    return o == null ? defaultValue : Convert.ToDateTime(o);
    } public byte Read(string section, string key, byte defaultValue)
    {
    object o = Read(section, key);
    return o == null ? defaultValue : Convert.ToByte(o);
    } public char Read(string section, string key, char defaultValue)
    {
    object o = Read(section, key);
    return o == null ? defaultValue : Convert.ToChar(o);
    } #endregion
    }
    }
      

  2.   

    如果是ms sql的话,它支持xml,比如你可以写语句select * from ... for xml,然后能直接得到xml字符串,另外可以先得到DataSet,然后DataSet.WriteXml.如果你要求的Xml格式比较特殊的话,你可以一个个Element地加,msdn中有例子
      

  3.   

    直接用DataSet.WriteXml(@"c:\123.xml");
      

  4.   

    你的格式不用一个节点一个节点的写
    直接用DataSet.WriteXml就可以了
    节点特殊的话才用CreatElementXmlElement xe = doc.CreatElement("col");
    xe.SetAttribute("name", "FWDF");
    xe.InnerText = "XXX";
    然后你把这个节点add进去就可以了
    或者用
    XmlNode node = doc.CreateNode(XmlNodeType.Element, "name", "");
      

  5.   

    DATASET 都是可以直接导出成XML 的,
      

  6.   

    如果你用dataset不能满足要求,那你就用下面的方法
    XmlElement xe = doc.CreatElement("col");
    xe.SetAttribute("name", "FWDF");
    xe.InnerText = "XXX";
    这个就是创建一个名称为col,有一个名字是name值是FWDF的属性,并且InnerText是XXX的节点
    这种方法是要一个节点一个节点的写
    就不能用ds.WriteXml了
    按照你的格式要求,你可以做个循环就可以实现了
    for(int i = 0;i < DS.Rows.Count;i++)
    {
        DataRow row = DS.Tables[0].Rows[i];
        XmlElement xe = doc.CreatElement("row");
        xe.SetAttribute("id", i.ToString());
        foreach(DataColumn myCol in DS.Tables[0].Columns)
        {
            XmlElement temp = doc.CreatElement("col");
            temp.SetAttribute("name", myCol.ColumnName);
            temp.InnerText = row[myCol];
            xe.AppendChild(temp);
        }
        //再把xe添加到ResultSet节点下
    }
    循环后doc.Save就可以了