簡體   English   中英

將數據庫寫入xml文件

[英]Write database into xml file

  1. 如何將數據庫導出為utf-8格式的.xml文件,因為我具有utf-8 BOM格式,但是我需要沒有BOM的數據庫

創建我的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();
    }
}
  1. 在我創建的.xml的第一行中,格式寫為“ utf-8”,但我需要不帶“-”的格式,我只需要“ utf8”

這是創建的.xml文件(左側是應該的樣子,右側是我創建的xml文件

  1. 在第一行中,如何將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM