日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

C#XmlDocument无法读取utf-16文件

發(fā)布時(shí)間:2023/12/18 C# 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#XmlDocument无法读取utf-16文件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
???????

使用 XmlDocument.Load(path); 方法來(lái)讀取一個(gè)encoding為utf-16的xml時(shí),就報(bào)了以上異常。

Xml文件:

[html] view plaincopy print?
  • <?xml?version="1.0"?encoding="utf-16"?>??
  • <DataProviders>??
  • ????<Provider??
  • ????????Name="A"??
  • ????????Type="OleDataProvider"??
  • ????????ConnectionString="Provider=SQLOLEDB.1;Password=xx;User?ID=sa;Data?Source=192.168.1.x\SQL2008;Initial?Catalog=xxx"?/>??
  • ????<Provider??
  • ????????Name="B"??
  • ????????Type="SubSonic.SqlDataProvider,?SubSonic"??
  • ????????ConnectionString="Password=xx;Persist?Security?Info=True;User?ID=sa;Database=xx;Data?Source=192.168.1.x\SQL2008"?/>??
  • </DataProviders>??
  • <?xml version="1.0" encoding="utf-16"?> <DataProviders><ProviderName="A"Type="OleDataProvider"ConnectionString="Provider=SQLOLEDB.1;Password=xx;User ID=sa;Data Source=192.168.1.x\SQL2008;Initial Catalog=xxx" /><ProviderName="B"Type="SubSonic.SqlDataProvider, SubSonic"ConnectionString="Password=xx;Persist Security Info=True;User ID=sa;Database=xx;Data Source=192.168.1.x\SQL2008" /> </DataProviders>在網(wǎng)上找了一下都不知道怎么解決。經(jīng)過摸索以及Msdn上的啟發(fā),得出以下解決方案:

    1. 將 uft-16 修改成 utf-8

    這是最簡(jiǎn)單的方式,但是由于我們項(xiàng)目中該xml文件已經(jīng)在客戶進(jìn)行了部署,并且系統(tǒng)其他地方也都有對(duì)該文件進(jìn)行操作,為了保證系統(tǒng)穩(wěn)定性,這種方式只能被拋棄。

    2. 將 xml 文件編碼修改成 Unicode

    既然是字符編碼導(dǎo)致,修改文件編碼也可以解決問題,但是仍然會(huì)影響穩(wěn)定性。

    3. 將 xml 文件以文本的方式讀取進(jìn)字符串,再通過 LoadXml 方式加載到 XmlDocument 中。

    [csharp] view plaincopy print?
  • string?xmlContent?=?File.ReadAllText(path);??
  • XmlDocument?xmlDoc?=?new?XmlDocument();??
  • xmlDoc.LoadXml(xmlContent);??
  • string xmlContent = File.ReadAllText(path); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlContent);


    參考:

    http://stackoverflow.com/questions/310669/why-does-c-sharp-xmldocument-loadxmlstring-fail-when-an-xml-header-is-included










    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    using System.Data; namespace AddMyUtility
    {
    ??? public class XmlOperateFuntion
    ??? {
    ??????? public static void singleCommandXmlDocumentCreateFunction(string path)//新加創(chuàng)建單條指令xml方法
    ??????? { XmlTextWriter xtw = new XmlTextWriter(path, Encoding.Unicode);
    ??????????? xtw.Formatting = Formatting.Indented;
    ??????????? xtw.WriteStartDocument();
    ??????????? xtw.WriteStartElement("SingleCommandList");
    ??????????? xtw.WriteAttributeString("xmlns:xsi", "http:www.w3.org/2001/XMLSchema-instance");
    ??????????? xtw.WriteStartElement("Commands");
    ??????????? xtw.WriteEndElement();
    ??????????? xtw.WriteEndElement();
    ??????????? xtw.WriteEndDocument();
    ??????????? xtw.Close(); }
    ??????? public static void commandChainXmlDocumentCreateFunction(string path)//新加創(chuàng)建指令鏈列表xml方法
    ??????? { XmlTextWriter xtw = new XmlTextWriter(path, Encoding.Unicode);
    ??????????? xtw.Formatting = Formatting.Indented;
    ??????????? xtw.WriteStartDocument();
    ??????????? xtw.WriteStartElement("CommandChainDocument");
    ??????????? xtw.WriteAttributeString("xmlns:xsi", "http:www.w3.org/2001/XMLSchema-instance");
    ??????????? xtw.WriteStartElement("CommandChainGroups");
    ??????????? xtw.WriteEndElement();
    ??????????? xtw.WriteEndElement();
    ??????????? xtw.WriteEndDocument();
    ??????????? xtw.Close(); }
    ??????? public static DataTable SearchFromXml(string filePath,string targetPath, string projectID)//單個(gè)衛(wèi)星單條指令xml文件返回?cái)?shù)據(jù)表
    ??????? {
    ??????????? string convertPath=changeEncodingMode(filePath, ref targetPath);
    ??????????? DataTable dt = new DataTable();
    ??????????? XmlTextReader reader = new XmlTextReader(convertPath);
    ??????????? List<singleCommandModelType> singleCommandList = new List<singleCommandModelType>();
    ??????????? singleCommandModelType scm = new singleCommandModelType();
    ??????????? while (reader.Read() != null)
    ??????????? {
    ??????????????? if (reader.NodeType == XmlNodeType.Element)
    ??????????????? {
    ??????????????????? if (reader.Name == "CommandText")
    ??????????????????? { scm.commandType = reader.ReadElementString().Trim(); }
    ??????????????????? if (reader.Name == "Description")
    ??????????????????? { scm.commandDescription = reader.ReadElementString().Trim(); } if (reader.Name == "Value")
    ??????????????????? {
    ??????????????????????? scm.commandValue = reader.ReadElementString().Trim();
    ??????????????????? }
    ??????????????????? if (reader.Name == "SendTime")
    ??????????????????? {
    ??????????????????????? scm.sendDatetime = DateTime.Parse(reader.ReadElementString().Trim());
    ??????????????????? } }
    ??????????????? if (reader.NodeType == XmlNodeType.EndElement)
    ??????????????? {
    ??????????????????? singleCommandList.Add(scm);
    ??????????????????? scm = new singleCommandModelType();
    ??????????????? } }
    ??????????? dt.Columns.Add("ID");
    ??????????? dt.Columns.Add("CommandType");
    ??????????? dt.Columns.Add("CommandValue");
    ??????????? dt.Columns.Add("CommandSendTime");
    ??????????? dt.Columns.Add("CommandBytes");
    ??????????? dt.Columns.Add("CommandProjectCode");
    ??????????? dt.Columns.Add("CommandTypeAndValue");
    ??????????? foreach (singleCommandModelType item in singleCommandList)
    ??????????? {
    ??????????????? dt.Rows.Add("", item.commandType, item.commandValue, item.sendDatetime, "", "", ""); }
    ??????????? reader.Close();
    ??????????? return dt;
    ??????? }
    ??????? public static DataTable SearchCommandResult(string commandSource, DataTable dt)//查詢指令返回表結(jié)果
    ??????? {
    ??????????? DataTable rsultDT = new DataTable();
    ??????????? rsultDT.Columns.Add("ID");
    ??????????? rsultDT.Columns.Add("CommandType");
    ??????????? rsultDT.Columns.Add("CommandValue");
    ??????????? rsultDT.Columns.Add("CommandSendTime");
    ??????????? rsultDT.Columns.Add("CommandBytes");
    ??????????? rsultDT.Columns.Add("CommandProjectCode");
    ??????????? rsultDT.Columns.Add("CommandTypeAndValue");
    ??????????? foreach (DataRow dr in dt.Rows)
    ??????????? {
    ??????????????? if (dr["CommandType"] == commandSource)
    ??????????????? {
    ??????????????????? rsultDT.Rows.Add(dr); } }
    ??????????? return rsultDT; }
    ??????? public static string changeEncodingMode(string sourcePath,ref string tempPath)//將UNICODE格式編碼轉(zhuǎn)換成UTF8格式
    ??????? {
    ??????? XmlDocument document = new XmlDocument();
    ??????? string content = System.IO.File.ReadAllText(sourcePath);
    ??????? content.Replace("utf-16", "Unicode");
    ??????????????????? document.LoadXml(content);
    ??????????????????? document.Save(tempPath);
    ??????????????????? return tempPath;
    ???????
    ??????? }
    ??????
    ??? } public class singleCommandModelType//單條指令類型
    ??? {
    ??????? public string commandType { get; set; }
    ??????? public string commandValue { get; set; }
    ??????? public string commandDescription { get; set; }
    ??????? public DateTime sendDatetime { get; set; } }
    }

    總結(jié)

    以上是生活随笔為你收集整理的C#XmlDocument无法读取utf-16文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。