C#XmlDocument无法读取utf-16文件
生活随笔
收集整理的這篇文章主要介紹了
C#XmlDocument无法读取utf-16文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
???????
<?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>在網上找了一下都不知道怎么解決。經過摸索以及Msdn上的啟發,得出以下解決方案: string?xmlContent?=?File.ReadAllText(path);?? XmlDocument?xmlDoc?=?new?XmlDocument();?? xmlDoc.LoadXml(xmlContent);?? string xmlContent = File.ReadAllText(path);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlContent);
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data; namespace AddMyUtility
{
??? public class XmlOperateFuntion
??? {
??????? public static void singleCommandXmlDocumentCreateFunction(string path)//新加創建單條指令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)//新加創建指令鏈列表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)//單個衛星單條指令xml文件返回數據表
??????? {
??????????? 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)//查詢指令返回表結果
??????? {
??????????? 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格式編碼轉換成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; } }
}
使用 XmlDocument.Load(path); 方法來讀取一個encoding為utf-16的xml時,就報了以上異常。
Xml文件:
[html] view plaincopy print?1. 將 uft-16 修改成 utf-8。
這是最簡單的方式,但是由于我們項目中該xml文件已經在客戶進行了部署,并且系統其他地方也都有對該文件進行操作,為了保證系統穩定性,這種方式只能被拋棄。
2. 將 xml 文件編碼修改成 Unicode。
既然是字符編碼導致,修改文件編碼也可以解決問題,但是仍然會影響穩定性。
3. 將 xml 文件以文本的方式讀取進字符串,再通過 LoadXml 方式加載到 XmlDocument 中。
[csharp] view plaincopy print?
參考:
http://stackoverflow.com/questions/310669/why-does-c-sharp-xmldocument-loadxmlstring-fail-when-an-xml-header-is-included
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data; namespace AddMyUtility
{
??? public class XmlOperateFuntion
??? {
??????? public static void singleCommandXmlDocumentCreateFunction(string path)//新加創建單條指令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)//新加創建指令鏈列表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)//單個衛星單條指令xml文件返回數據表
??????? {
??????????? 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)//查詢指令返回表結果
??????? {
??????????? 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格式編碼轉換成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; } }
}
總結
以上是生活随笔為你收集整理的C#XmlDocument无法读取utf-16文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 缴满15年能领多少钱 养老金计算公式网上
- 下一篇: 笔记本电池的正确使用方法