使用 XmlDocument.Load(path); 方法来读取一个encoding为utf-16的xml时,就报了以上异常。
Xml文件:
- <?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>
<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>
在网上找了一下都不知道怎么解决。经过摸索以及Msdn上的启发,得出以下解决方案:
1. 将 uft-16 修改成 utf-8。
这是最简单的方式,但是由于我们项目中该xml文件已经在客户进行了部署,并且系统其他地方也都有对该文件进行操作,为了保证系统稳定性,这种方式只能被抛弃。
2. 将 xml 文件编码修改成 Unicode。
既然是字符编码导致,修改文件编码也可以解决问题,但是仍然会影响稳定性。
3. 将 xml 文件以文本的方式读取进字符串,再通过 LoadXml 方式加载到 XmlDocument 中。
- 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;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
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方法
{
{
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();
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方法
{
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();
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(); }
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());
}
{
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();
}
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, "", "", "");
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;
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;
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 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; }
{
public string commandType { get; set; }
public string commandValue { get; set; }
public string commandDescription { get; set; }
public DateTime sendDatetime { get; set; }
}
}
}