[英]Write database into xml file
創建我的xml文件的代碼是:
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
settings.NewLineOnAttributes = true;
using (XmlWriter xmlWriter = XmlWriter.Create(saveFileDialog1.FileName, settings))
{
dt.WriteXml(xmlWriter);
xmlWriter.Close();
}
}
DocumentElement
更改為table name = "UnknownTable"
? 嘗試這個
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable("UnknownTable");
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("ticket-id", typeof(int));
dt.Rows.Add(new object[] { 11528, 4917 });
dt.Rows.Add(new object[] { 11529, 4918 });
string identification =
"<?xml version=\"1.0\" encoding=\"utf8\"?>" +
"<table name=\"UnknownTable\">" +
"</table>";
XDocument doc = XDocument.Parse(identification);
XElement table = doc.Root;
foreach (DataRow row in dt.AsEnumerable())
{
XElement newRow = new XElement("row");
table.Add(newRow);
for (int colNum = 0; colNum < dt.Columns.Count; colNum++)
{
string name = dt.Columns[colNum].ColumnName;
newRow.Add(new XElement(name, row[colNum]));
}
}
}
}
}
1.要創建沒有BOM的編碼,請使用:
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
2.根據標准必須是utf-8,帶連字符。
3.指定數據表的名稱:
DataTable dt = new DataTable("UnknownTable");
要么
dt.TableName = "UnknownTable";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.