1

我尝试了一些将新子添加到 XML 文件的代码。我注意到使用 XmlDocument.Load(String filename) 和 XmlDocument.Load(FileStream fs) 时结果不同。下面显示了原始 XML 文件数据。

<?xml version="1.0" encoding="utf-8"?>
<grandparent>
    <parent>
        <child>
            <grandchild>some text here</grandchild>
        </child>
        <child>
            <grandchild>another text here</grandchild>
        </child>
    </parent>
</grandparent>

下面显示了使用 XmlDocument.Load(String filename) 附加子元素的 C# 代码

XmlDocument doc = new XmlDocument();
doc.Load(filename);

XmlNode child= doc.CreateNode(XmlNodeType.Element, "child", null);
XmlNode grandchild = doc.CreateNode(XmlNodeType.Element, "grandchild", null);
grandchild.InnerText = "different text here";
child.AppendChild(grandchild);
doc.SelectSingleNode("//grandparent/parent").AppendChild(child);
doc.Save(filename);

结果 XML 文件工作正常,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<grandparent>
    <parent>
        <child>
            <grandchild>some text here</grandchild>
        </child>
        <child>
            <grandchild>another text here</grandchild>
        </child>
        <child>
            <grandchild>different text here</grandchild>
        </child>
    </parent>
</grandparent>

但是,如果我要使用 XmlDocument.Load(FileStream fs) 如下所示

FileStream fs = new FileStream(filename, FileMode.Open)
XmlDocument doc = new XmlDocument();
doc.Load(fs);

XmlNode child= doc.CreateNode(XmlNodeType.Element, "child", null);
XmlNode grandchild = doc.CreateNode(XmlNodeType.Element, "grandchild", null);
grandchild.InnerText = "different text";
child.AppendChild(grandchild);
doc.SelectSingleNode("//grandparent/parent").AppendChild(child);
doc.Save(fs);
fs.Close();

结果 XML 文件会很奇怪,就像再次复制整个 XML 文件一样,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<grandparent>
    <parent>
        <child>
            <grandchild>some text here</grandchild>
        </child>
        <child>
            <grandchild>another text here</grandchild>
        </child>
    </parent>
</grandparent><?xml version="1.0" encoding="utf-8"?>
<grandparent>
    <parent>
        <child>
            <grandchild>some text here</grandchild>
        </child>
        <child>
            <grandchild>another text here</grandchild>
        </child>
        <child>
            <grandchild>different text here</grandchild>
        </child>
    </parent>
</grandparent>

有人能告诉我为什么吗?提前致谢。

4

1 回答 1

5

调用 XmlDocument.Save(FileStream fs) 会将 XmlDocument 数据附加到流中。

前面的 XmlDocument.Load(FileStream fs) 调用,在同一个 FileStream 实例上,将导致 FileStream 的位置偏移原始 xml 文件中的字节数。因此,在此 FileStream 实例上执行的任何附加操作都将在数据读入之后进行。为了应对这种情况,您需要重置 FileStream 的位置。

要重置 FileStream 实例的位置,请使用:

... FileStream fs ...
... XmlDocument doc ...

fs.SetLength(0); //Optional: Clears the file on disk
fs.Flush(); //Optional: Flushes the stream to write the clear to disk
fs.Position = 0; //Resets the position of the stream
doc.Save(fs); //Save the XmlDocument to the FileStream

编辑:两种 FileStream 方法。注意,在调用 XmlDocument.Save 之前,我已将 FileMode 更改为 FileMode.Create;这将创建一个全新的文件(清除文件的内容)

FileStream fs = null;
XmlDocument doc = new XmlDocument();

using (fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
    doc.Load(fs);
}

//Do stuff to the xmlDoc

using (fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
    doc.Save(fs);
}
于 2013-10-29T04:44:10.240 回答