如何将xmldocument保存到流中

use*_*383 12 c# xmldocument xmlreader

我已经编写了代码来解析我的xml文件,XmlReader所以我不想重写它.我现在已经为程序添加了加密功能.我有encrypt()和decrypt()函数,它们采用xml文档和加密算法.我有一个使用xml阅读器来解析文件的函数,但现在使用xml文档我不知道如何创建xmlreader.

问题是如何将我的xml文档保存到流.我确信它很简单,但我对流不了解.

XmlDocument doc = new XmlDocument();
        doc.PreserveWhitespace = true;
        doc.Load(filep);
        Decrypt(doc, key);

        Stream tempStream = null;
        doc.Save(tempStream);   //  <--- the problem is here I think

        using (XmlReader reader = XmlReader.Create(tempStream))  
        {
            while (reader.Read())
            { parsing code....... } }
Run Code Online (Sandbox Code Playgroud)

Agh*_*oub 39

你可以尝试MemoryStream上课

XmlDocument xmlDoc = new XmlDocument( ); 
MemoryStream xmlStream = new MemoryStream( );
xmlDoc.Save( xmlStream );

xmlStream.Flush();//Adjust this if you want read your data 
xmlStream.Position = 0;

//Define here your reading
Run Code Online (Sandbox Code Playgroud)

  • 根据[关于Flush()方法的文档](http://msdn.microsoft.com/en-us/library/system.io.memorystream.flush%28v=vs.110%29.aspx):`因为写入MemoryStream对象的任何数据都写入RAM,这种方法是多余的.应该从你的答案中删除它,因为它本质上是一个"noop". (4认同)