Xml.load(_stream) 在 c# 中失败 - 根级别的数据无效。第 1 行,位置 1
Xml.load(_stream) in c# failed - Data at the root level is invalid. Line 1, position 1
无论我加载哪个 xml 文档,它都会给我同样的错误。我删除了声明,检查了 BOM 错误 - 一切正常 - 我无法解决此问题:
internal int ProcessData()
{
// xmldocument
var xmlDoc = new XmlDocument();
// make sure there is actually data in this file
try
{
if (_stream.Length <= 0)
{
// TODO - add error handling condition where the stream has no data
}
else
{
// check if file has an excel signature (OOXML file signature) - we only accept XLSX and .XML on upload
//so we can safely make the assumption here as to what to do based on the file type
var b = new byte[8];
_stream.Read(b, 0, 8);
// if Excel then convert data to XML format
if (ExcelMagicNumberString == BitConverter.ToString(b))
{
// convert the file to XML
xmlDoc = ConvertExcelToXml();
// clean up the stream
_stream.Close();
}
else
{
// load the Xml document from the database/memory
// xmlDoc.Load(_stream);
// string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
// if (xml.StartsWith(_byteOrderMarkUtf8))
// {
// xml.Remove(0, _byteOrderMarkUtf8.Length);
xmlDoc.Load(_stream);
// }
GetLeadingIndicatorType(xmlDoc);
}
xml 文档:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
我能够修复它。没有 xml 加载 xml.load(_stream) 的原因是因为它(流)读取前 8 位 - 所以当需要调用加载时 - 它从位置 8 开始,而不是位置 0,因此无法读取 xml 声明或 xml 文档的根目录。通过在调用加载方法之前添加这两行,它起作用了。:
// load the Xml document from the database/memory
_stream.Position = 0;
_stream.Read(b, 0, 0);
xmlDoc.Load(_stream);
无论我加载哪个 xml 文档,它都会给我同样的错误。我删除了声明,检查了 BOM 错误 - 一切正常 - 我无法解决此问题:
internal int ProcessData()
{
// xmldocument
var xmlDoc = new XmlDocument();
// make sure there is actually data in this file
try
{
if (_stream.Length <= 0)
{
// TODO - add error handling condition where the stream has no data
}
else
{
// check if file has an excel signature (OOXML file signature) - we only accept XLSX and .XML on upload
//so we can safely make the assumption here as to what to do based on the file type
var b = new byte[8];
_stream.Read(b, 0, 8);
// if Excel then convert data to XML format
if (ExcelMagicNumberString == BitConverter.ToString(b))
{
// convert the file to XML
xmlDoc = ConvertExcelToXml();
// clean up the stream
_stream.Close();
}
else
{
// load the Xml document from the database/memory
// xmlDoc.Load(_stream);
// string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
// if (xml.StartsWith(_byteOrderMarkUtf8))
// {
// xml.Remove(0, _byteOrderMarkUtf8.Length);
xmlDoc.Load(_stream);
// }
GetLeadingIndicatorType(xmlDoc);
}
xml 文档:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
我能够修复它。没有 xml 加载 xml.load(_stream) 的原因是因为它(流)读取前 8 位 - 所以当需要调用加载时 - 它从位置 8 开始,而不是位置 0,因此无法读取 xml 声明或 xml 文档的根目录。通过在调用加载方法之前添加这两行,它起作用了。:
// load the Xml document from the database/memory
_stream.Position = 0;
_stream.Read(b, 0, 0);
xmlDoc.Load(_stream);