XmlDocument.Load(String filename) 和 XmlDocument.Load(FileStr

本文关键字:XmlDocument Load FileStr String filename | 更新日期: 2023-09-27 18:37:08

我尝试了一些将新子级添加到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(字符串文件名)追加子元素的 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>

有人可以告诉我为什么吗?提前谢谢。

XmlDocument.Load(String filename) 和 XmlDocument.Load(FileStr

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

之前在同一个 FileStream 实例上的 XmlDocument.Load(FileStream fs) 调用将导致 FileStream 的位置偏移原始 xml 文件中的字节数。因此,在此 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

编辑:两个文件流方法。请注意,在调用 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);
}