首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于XmlDocument的load方法发生错误,“缺少根元素”

2012-03-19 
关于XmlDocument的load方法发生异常,“缺少根元素”下面这段代码为什么发生异常C# codeusing ( Streamstream

关于XmlDocument的load方法发生异常,“缺少根元素”
下面这段代码为什么发生异常

C# code
            using ( Stream  stream = new MemoryStream())            {                XmlSerializer xmlSerializer = new XmlSerializer(typeof(test));                xmlSerializer.Serialize(stream, instance);                                XmlDocument objectDoc = new XmlDocument();                [color=#FF0000]objectDoc.Load(stream);  //发生异常,XmlException 缺少根元素。[/color]                                                XmlNode node = objectDoc.SelectSingleNode(typeof(T).Name);                InsertToConfigFile(node);            }


[解决办法]

在xmldocument.Load(mstream)前加上
mstream.Position = 0;
或者
mstream.Seek(0, SeekOrigin.Begin);

热点排行