不管我加载的是哪个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>发布于 2015-03-10 17:37:00
我能修好它。没有使用xml.load(_stream)加载xml的原因是,它(流)读取前8位数据--所以当需要调用加载时--从位置8开始,而不是从位置0开始,因此无法读取xml声明或xml文档的根。通过在调用load方法之前添加这两行,它起了作用:
// load the Xml document from the database/memory
_stream.Position = 0;
_stream.Read(b, 0, 0);
xmlDoc.Load(_stream);https://stackoverflow.com/questions/28952529
复制相似问题