如果现有XML文件已存在,则覆盖它

时间:2012-08-14 16:34:35

标签: c# xml filestream file-permissions

我正在尝试覆盖现有的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);
 }

3 个答案:

答案 0 :(得分:3)

尝试写一下这样的文件:

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();
         }
     }     
 }

答案 1 :(得分:0)

正如@Tommy所提到的 - 我无法看到代码,处理FileStream,我认为在using语句中包装外部资源总是更好。 除此之外,可能会发生以下命令吗?

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

答案 2 :(得分: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();
    }
}

我希望这有帮助!

相关问题