【问题标题】:Overwrite existing XML file if it alreadly exists如果现有 XML 文件已存在,则覆盖它
【发布时间】:2012-08-11 00:16:25
【问题描述】:

如果现有的 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 数据的部分在这里似乎很重要......

标签: c# xml filestream file-permissions


【解决方案1】:

尝试像这样写入文件:

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】:

    正如@Tommy 提到的-我看不到代码,处理FileStream,我认为将外部资源包装在 using 语句中总是更好。 除此之外,是否会发生以下顺序?

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

    【讨论】:

      【解决方案3】:

      我这样做了,无论您使用哪个 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();
          }
      }
      

      我希望这会有所帮助!

      【讨论】:

      • 我得到“无法将 'System.Xml.Linq.XText' 类型的对象转换为 'System.Xml.Linq.XElement' 类型。”而转换为 xdocument 的 xmldocument 文件
      猜你喜欢
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 2020-08-18
      • 1970-01-01
      • 2015-02-02
      • 2022-08-02
      • 1970-01-01
      相关资源
      最近更新 更多