我在将数据集转成xml文件的时候,只是将表名和字段,字段值获取了,但是转成xml文件的时候却出现了DocumentElement,怎么将它删掉?
可以通过 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>
谢谢你啊,已经解决了,两种方法
第一种 /// /// 重写文档,去掉 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(); }
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(); }
第二种
/// <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(); }
建议提供相关代码
– dudu 4年前@dudu: 好了,由于是远程开发,所以无法粘贴代码
– 夕阳下的你我 4年前@dudu: 我截图了
– 夕阳下的你我 4年前