首页新闻找找看学习计划

C# 数据集转成xml文件如何删掉<DocumentElement>?

0
悬赏园豆:30 [已解决问题] 解决于 2019-12-05 16:17

我在将数据集转成xml文件的时候,只是将表名和字段,字段值获取了,但是转成xml文件的时候却出现了DocumentElement,怎么将它删掉?

 

夕阳下的你我的主页 夕阳下的你我 | 初学一级 | 园豆:114
提问于:2019-12-05 10:16

建议提供相关代码

dudu 1个月前

@dudu: 好了,由于是远程开发,所以无法粘贴代码

夕阳下的你我 1个月前

@dudu: 我截图了

夕阳下的你我 1个月前
< >
分享
最佳答案
0

可以通过 DataSet 的名称修改 DocumentElement 的名称,比如

class Program
{
    static void Main(string[] args)
    {
        var ds = new DataSet("Q124631");
        var dt = new DataTable("Barcode");
        dt.Columns.Add("Status", typeof(string));
        var dr = dt.NewRow();
        dr["Status"] = "Coding";
        dt.Rows.Add(dr);
        ds.Tables.Add(dt);
        dt.WriteXml("temp.xml");
    }
}

生成的 xml 内容如下

<?xml version="1.0" standalone="yes"?>
<Q124631>
  <Barcode>
    <Status>Coding</Status>
  </Barcode>
</Q124631>
收获园豆:30
dudu | 高人七级 |园豆:39596 | 2019-12-05 16:06

谢谢你啊,已经解决了,两种方法

第一种 /// /// 重写文档,去掉 DocumentElement 节点 /// ///文档完整路径 private void ReWrite( string fileName) { FileStream fs = null; string readStr = ""; //读取 try { fs = new FileStream(fileName, FileMode.OpenOrCreate); byte[] s = new byte[1000]; fs.Read(s, 0, 1000); readStr = Encoding.UTF8.GetString(s); fs.Close(); } catch (Exception e) { Console.WriteLine(e.Message.ToString()); } //替换 readStr = readStr.Replace("\r\n",""); readStr = readStr.Replace("\r\n", ""); readStr = readStr.Replace("", ""); readStr = readStr.Replace("", ""); //写入 try { fs = new FileStream(fileName, FileMode.OpenOrCreate); //fs = new FileStream(fileName, FileMode.Append); string writeStr = readStr;//"asdf"; fs.Write(Encoding.UTF8.GetBytes(writeStr), 0, writeStr.Length); fs.Close(); } catch (Exception e) { Console.WriteLine(e.Message.ToString()); } fs.Close(); } 第二种: private void DataTableToXMLDoc(DataTable dt,string fileName) { string writeStr = string.Empty;//写入文档的内容; DataRow drRow = dt.Rows[0]; //组合body foreach (DataColumn dc in dt.Columns) { writeStr += string.Format(" <{0}>{1}\r\n",dc.ColumnName, drRow[dc.ColumnName]); } //加上头尾 writeStr = string.Format("<{0}>\r\n{1}", dt.TableName, writeStr); //输出到文件 FileStream fs = null; try { fs = new FileStream(fileName, FileMode.OpenOrCreate); //fs = new FileStream(fileName, FileMode.Append); byte[] cWrite = Encoding.UTF8.GetBytes(writeStr); fs.Write(cWrite, 0, cWrite.Length); fs.Close(); } catch (Exception e) { Console.WriteLine(e.Message.ToString()); } fs.Close(); }

夕阳下的你我 | 园豆:114 (初学一级) | 2019-12-05 16:13
private void DataTableToXMLDoc(DataTable dt,string fileName)
        {
            string writeStr = string.Empty;//写入文档的内容;
            
            DataRow drRow = dt.Rows[0];
            //组合body
            foreach (DataColumn dc in dt.Columns)
            {
                writeStr += string.Format("  <{0}>{1}</{0}>\r\n",dc.ColumnName, drRow[dc.ColumnName]);
            }

            //加上头尾
            writeStr = string.Format("<{0}>\r\n{1}</{0}>", dt.TableName, writeStr);


            //输出到文件
            FileStream fs = null;
            try
            {
                fs = new FileStream(fileName, FileMode.OpenOrCreate);
                //fs = new FileStream(fileName, FileMode.Append);
                byte[] cWrite = Encoding.UTF8.GetBytes(writeStr);
                fs.Write(cWrite, 0, cWrite.Length);
                fs.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }
            fs.Close();

        }
夕阳下的你我 | 园豆:114 (初学一级) | 2019-12-05 16:13

第二种

/// <summary>
        /// 重写文档,去掉 DocumentElement 节点
        /// </summary>
        /// <param name="fileName">文档完整路径</param>
        private void ReWrite( string fileName)
        {
            FileStream fs = null;

            string readStr = "";
            //读取
            try
            {
                fs = new FileStream(fileName, FileMode.OpenOrCreate);
                byte[] s = new byte[1000];
                fs.Read(s, 0, 1000);
                readStr = Encoding.UTF8.GetString(s);
                
                fs.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }

            //替换
            readStr = readStr.Replace("<DocumentElement>\r\n","");
            readStr = readStr.Replace("</DocumentElement>\r\n", "");
            readStr = readStr.Replace("<DocumentElement>", "");
            readStr = readStr.Replace("</DocumentElement>", "");

            //写入
            try
            {
                fs = new FileStream(fileName, FileMode.OpenOrCreate);
                //fs = new FileStream(fileName, FileMode.Append);
                string writeStr = readStr;//"asdf";
                fs.Write(Encoding.UTF8.GetBytes(writeStr), 0, writeStr.Length);
                fs.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }
            fs.Close();

        }

 

夕阳下的你我 | 园豆:114 (初学一级) | 2019-12-05 16:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册