我在解析具有utf-16编码的XML时遇到了问题,但它完美无缺
用utf-8罚款。
有没有人可以帮我解决这个问题?
我收到以下错误:
System.Web.HttpUnhandledException' was thrown.
System.Xml.XmlException: There is no Unicode byte order mark.
Cannot switch to Unicode
XML标头:
<?xml version="1.0" encoding="utf-16"?>
<RiskAssessmentRequestValue xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
C#代码背后:
rptTransformedXml.DataSource = parser.ExtractData(xml);
rptTransformedXml.DataBind();
public List<XmlDataExtract> ExtractData(string xml)
{
MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(xml));
return ExtractData(stream);
}
public List<XmlDataExtract> ExtractData(Stream xmlStream)
{
XmlReaderSettings settings = new XmlReaderSettings
{
IgnoreComments = true,
IgnoreWhitespace = true,
CloseInput = true
};
XmlReader reader = XmlReader.Create(xmlStream, settings);
XmlPathBuilder pathBuilder = new XmlPathBuilder(reader);
List<XmlDataExtract> xmlDataList = new List<XmlDataExtract>();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.XmlDeclaration)
continue;
pathBuilder.Add();
CollectAttributeData(reader, xmlDataList, pathBuilder);
CollectElementData(reader, xmlDataList, pathBuilder);
}
return xmlDataList;
}
答案 0 :(得分:3)
您可以根据xml内容的编码创建编码器:
string encoding = "UTF-8"; // should match encoding in XML
string xml = @"<?xml version='1.0' encoding='UTF-8'?><table><row>1</row></table>";
var ms = new MemoryStream(Encoding.GetEncoding(encoding).GetBytes(xml));
var xdrs = new XmlReaderSettings()
{IgnoreComments = true,
IgnoreWhitespace = true,
CloseInput = true};
var xdr = XmlReader.Create(ms, xdrs);
while (xdr.Read())
{
Console.Write("qqq");
}
有关编码的详细信息,有一个相关的question