c中的Xml.load(_stream)失败-根级别的数据无效。第1行,位置1

c中的Xml.load(_stream)失败-根级别的数据无效。第1行,位置1,xml,c#-4.0,Xml,C# 4.0,无论加载哪个xml文档,它都会给我相同的错误。我已删除声明,我已检查BOM错误-一切正常-我无法解锁此问题: internal int ProcessData() { // xmldocument var xmlDoc = new XmlDocument(); // make sure there is actually data in this file try { if (

无论加载哪个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);
                }
internal int ProcessData()
{
//XML文档
var xmlDoc=新的XmlDocument();
//确保此文件中确实有数据
尝试
{

如果(_stream.Length我能够修复它。没有xml与xml.load(_stream)一起加载的原因是它(流)读取前8位,因此在调用load时-它从位置8开始,而不是从位置0开始,因此无法读取xml声明或xml文档的根。通过在调用load方法之前添加这两行,它起到了作用:

                    // load the Xml document from the database/memory
                        _stream.Position = 0;
                        _stream.Read(b, 0, 0);
                          xmlDoc.Load(_stream);

您的XML文件很可能无效。您可以发布它(或代表性示例)吗?xml是有效的。我已经在xml编辑器上对其进行了测试。我正在使用最基本的一个作为测试,但它仍然会给我带来问题:我已经根据您的请求添加了一个示例。抱歉,这不是突然的意思-再次学习如何使用SOF。这可能是编码问题吗?如果您取消对处理UTF8的代码的注释,会发生什么?相同的错误?我已经测试过h注释、取消注释和删除声明(处理UTF8的代码),我仍然会收到相同的错误。
                    // load the Xml document from the database/memory
                        _stream.Position = 0;
                        _stream.Read(b, 0, 0);
                          xmlDoc.Load(_stream);