Overwrite existing XML file if it alreadly exists

点点圈 提交于 2019-12-10 18:45:59

问题


I am trying to overwrite an existing xml file if it already exists.

I am using the code below to check if the file exists and then overwrite it if it does. The existing file is hidden so I am unhiding it before attempting to overwrite.

The changes are not occuring to the file and the overwriting is not working however.

Here is the code I am using below minus the part where I am writing the new xml data.

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

回答1:


Try writing to the file like this :

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



回答2:


As @Tommy mentioned - I cant see code, disposing the FileStream, I think it's always better to wrap external resources in using statement. Apart from that can it be that the following order takes place?

  1. You create xml file for the first time
  2. You try to recreate same file in the same session.
  3. FileStream from p.1 is still locking the file



回答3:


I did this, which keeps everything dynamic no matter which XDocument you are using:

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

I hope this helps!



来源:https://stackoverflow.com/questions/11956819/overwrite-existing-xml-file-if-it-alreadly-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!