【发布时间】:2012-08-07 22:15:46
【问题描述】:
我有一个包含 xml 文件的 zip 文件, 我正在将此 xml 文件加载到 xml 文档中,而无需提取文件。 这是通过流完成的。 这样做之后,我正在修改一些节点的内部文本。 问题是我在尝试保存流后遇到了前面提到的异常,代码如下:
(我在这里使用 DotNetZip)
ZipFile zipFile = ZipFile.Read(zipPath); // the path is my desktop
foreach (ZipEntry entry in zipFile)
{
if (entry.FileName == "myXML.xml")
{
//creating the stream and loading the xml doc from the zip file:
Stream stream = zipFile[entry.FileName].OpenReader();
XmlReader xReader = XmlReader.Create(stream);
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xReader);
//changing the inner text of the doc nodes:
xDoc.DocumentElement.SelectSingleNode("Account/Name").InnerText = "VeXe";
xDoc.DocumentElement.SelectSingleNode("Account/Money").InnerText = "Million$";
xDoc.Save(stream); // here's where I got the exception.
break;
}
}
我不是专业编码人员,但我注意到它也可以将XmlWriter 作为参数而不是xDoc.Save(stream);,因此我尝试在实例化 XmlReader 后立即创建 XmlWriter 的实例 ..
我试过这样做:xDoc.Save(XmlWriter)
我遇到了一个例外,比如:“阅读后无法写作”
我怎样才能成功保存 xDoc ?
添加: 我有一个想法,将 xml 文件保存在其他地方,比如临时文件夹或其他地方 然后在 zip 中添加该保存的文件,覆盖旧文件,然后删除 temp 中的 xml 文件。 但这不是我想要的,我想直接处理 zip 文件,进出,没有第三方。
【问题讨论】: