首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Xml.load(_stream)在c#失败-根级别的数据无效。第1行,位置1

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

Stack Overflow用户
提问于 2015-03-09 21:59:30
回答 1查看 617关注 0票数 0

不管我加载的是哪个xml文档,它都给了我同样的错误。我删除了声明,检查了BOM错误--一切都很好--我无法解开这个问题:

代码语言:javascript
运行
复制
    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文档:

代码语言:javascript
运行
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-10 17:37:00

我能修好它。没有使用xml.load(_stream)加载xml的原因是,它(流)读取前8位数据--所以当需要调用加载时--从位置8开始,而不是从位置0开始,因此无法读取xml声明或xml文档的根。通过在调用load方法之前添加这两行,它起了作用:

代码语言:javascript
运行
复制
                    // load the Xml document from the database/memory
                        _stream.Position = 0;
                        _stream.Read(b, 0, 0);
                          xmlDoc.Load(_stream);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28952529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档