如果XML文件已经存在,覆盖现有的XML文件。

5

我想要覆盖一个已经存在的xml文件。

我使用下面的代码来检查文件是否存在,如果存在就覆盖它。现有的文件是隐藏的,所以在尝试覆盖之前我要先显示它。

然而,对文件进行更改和覆盖并没有起作用。

以下是我使用的代码(不包含我写新的XML数据的部分):

if(File.Exists(filePath))
{
     File.SetAttributes(filePath,FileAttributes.Normal);
     FileIOPermission filePermission = 
              new FileIOPermission(FileIOPermissionAccess.AllAccess,filePath);

     FileStream fs = new FileStream(filePath, FileMode.Create);

     XmlWriter w = XmlWriter.Create(fs);
 }

在这里编写新的 XML 数据似乎很重要... - msmucker0527
3个回答

4
尝试按照以下方式向文件写入内容:
if(File.Exists(filePath))
{
     File.SetAttributes(filePath,FileAttributes.Normal);
     FileIOPermission filePermission = 
              new FileIOPermission(FileIOPermissionAccess.AllAccess,filePath);

     using(FileStream fs = new FileStream(filePath, FileMode.Create))
     {
         using (XmlWriter w = XmlWriter.Create(fs))
         {
             w.WriteStartElement("book");
             w.WriteElementString("price", "19.95");
             w.WriteEndElement();
             w.Flush();
         }
     }     
 }

0

我做了这个,无论你使用哪个XDocument,它都可以保持动态:

private void WriteToXml(XDocument xDoc, string filePath)
{
    // Gets the root XElement of the XDocument
    XElement root = xDoc.Root;

    // Using a FileStream for streaming to a file:
    // Use filePath.
    // If it's a new XML doc then create it, else open it.
    // Write to file.
    using (FileStream writer = 
           new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
    {
        // For XmlWriter, it uses the stream that we created: writer.
        using (XmlWriter xmlWriter = XmlWriter.Create(writer))
        {
            // Creates a new XML file. The false is for "StandAlone".
            xmlWriter.WriteStartDocument(false);

            // Writes the root XElement Name.
            xmlWriter.WriteStartElement(root.Name.LocalName);

            // Foreach parent XElement in the Root...
            foreach (XElement parent in root.Nodes())
            {
                // Write the parent XElement name.
                xmlWriter.WriteStartElement(parent.Name.LocalName);

                // Foreach child in the parent...
                foreach (XElement child in parent.Nodes())
                {
                    // Write the node with XElement name and value.
                    xmlWriter.WriteElementString(child.Name.LocalName, child.Value);
                }
                // Close the parent tag.
                xmlWriter.WriteEndElement();
            }
            // Close the root tag.
            xmlWriter.WriteEndElement();
            // Close the document.
            xmlWriter.WriteEndDocument();

            // Good programming practice, manually flush and close all writers
            // to prevent memory leaks.
            xmlWriter.Flush();
            xmlWriter.Close();
        }
        // Same goes here.
        writer.Flush();
        writer.Close();
    }
}

希望这能帮到你!


当我将一个xmldocument文件转换为xdocument时,出现了“无法将类型为'System.Xml.Linq.XText'的对象强制转换为类型'System.Xml.Linq.XElement'。”的错误。 - usefulBee

0

正如 @Tommy 所提到的 - 我看不到代码,销毁 FileStream,我认为最好总是将外部资源包装在 using 语句中。 除此之外,以下顺序可能发生吗?

  1. 您第一次创建 XML 文件
  2. 您尝试在同一会话中重新创建相同的文件。
  3. p.1 中的 FileStream 仍然锁定该文件

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接