0
if (!File.Exists(signatureFile))
        {

            XElement signature;

            try
            {

                XNamespace ns = "http://www.w3.org/2000/09/xmldsig#";

                signature = new XElement("DocumentSignature", new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"), new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"),
                new XElement("Files", new XAttribute("id", "files"),
                        new XElement("File", new XAttribute("file_name", file), new XAttribute("digestmethod", "http://www.w3.org/2000/09/xmldsig#sha1"), new XAttribute("archive_path", "data/" + file), "/E06svX3vAnvYnnGlMT9auuh8uE=")),
                    //new XElement("File", new XAttribute("file_name", file), new XAttribute("digestmethod", "http://www.w3.org/2000/09/xmldsig#sha1"), new XAttribute("archive_path", "data/"  + file), "x5kx5oXhxZ/lLGZ0iLTFLL3XVig=")),
                    new XElement("Attributes",
                        new XElement("Attribute", new XAttribute("id", "ad_ret_policy"), new XAttribute("system_attribute", "false"), new XAttribute("readonly", "false"),
                            new XElement("Value", metadata["Retention Policy"])),
                        new XElement("Attribute", new XAttribute("id", "ad_keepyears"), new XAttribute("system_attribute", "false"), new XAttribute("readonly", "false"),
                            new XElement("Value", metadata["Keep years"])),
                        new XElement("Attribute", new XAttribute("id", "ad_classification"), new XAttribute("system_attribute", "false"), new XAttribute("readonly", "false"),
                            new XElement("Value", metadata["Classification plan"])),
                        new XElement("Attribute", new XAttribute("id", "ad_permanent"), new XAttribute("system_attribute", "false"), new XAttribute("readonly", "false"),
                            new XElement("Value", metadata["Permanent"])),
                        new XElement("Attribute", new XAttribute("id", "ad_keywords"), new XAttribute("system_attribute", "false"), new XAttribute("readonly", "false"),
                            new XElement("Value", metadata["Keywords"])),
                        new XElement("Attribute", new XAttribute("id", "ad_archive"), new XAttribute("system_attribute", "false"), new XAttribute("readonly", "false"),
                            new XElement("Value", metadata["Archive"])),
                        new XElement("Attribute", new XAttribute("id", "ad_is_long_term_format"), new XAttribute("system_attribute", "false"), new XAttribute("readonly", "false"),
                            new XElement("Value", "false"))));                                        //END OF ATTRIBUTES SNIPPET

                signature.Save(signatureFile);
            }
            catch (Exception ex)
            {
                ex.ToString();
                Program.logger.Error(ex);
                throw;
            }
        }
        else
        {
            XDocument doc = XDocument.Load(signatureFile);

            XElement items = doc.Root.Descendants("File").FirstOrDefault();
            items.ReplaceWith(new XElement("File", new XAttribute("file_name", file), new XAttribute("digestmethod", "http://www.w3.org/2000/09/xmldsig#sha1"), 
                new XAttribute("archive_path", "data/" + file), "/E06svX3vAnvYnnGlMT9auuh8uE="));

            doc.Save("C:\\Docs\\modified.xml");
        }

I am having problem with saving the document. It returns me an exception saying that file is being used by another process. I tried using FileStream also, with no success. I need to use the same document as specified. I also tried deleting the file after it's done being used by another method, with the same error. Saving it with date and time makes it unique and it works tho, but its hard to use it in another method in this kind of format. Any ideas?

Lennart
  • 9,657
  • 16
  • 68
  • 84
  • 1
    There's nothing in the code you posted that would keep the file open. Therefore, the problem must lie elsewhere. – Matthew Watson Aug 30 '18 at 07:45
  • @MatthewWatson Like i said, I am using the file in another method. This is because it needs to be digitally signed with certificate, and every file has its own document that needs to be set in this xml. Thats why i'm always creating new xml. It throws me an exception while saving the document in the line doc.Save("C:\\Docs\\modified.xml");. – Mark Dolenc Aug 30 '18 at 07:54
  • Maybe I didnt clarify it enough. I need a new xml for every file i get. I dont need to open the XML here, i just need to create new one and access it later in another method. But exception is being thrown at doc.Save("") or if i try to delete the file in another method with File.Delete after I'm done using it. – Mark Dolenc Aug 30 '18 at 07:57
  • What method do you use to read the file? – Hasan Emrah Süngü Aug 30 '18 at 08:02
  • 1
    im calling SignXmlFile() which takes the file with doc = new XmlDocument() and doc.Load(new XmlTextReader(FileName)) – Mark Dolenc Aug 30 '18 at 08:06
  • @MarkDolenc please have look at my answer. I think it should solve your problem – Hasan Emrah Süngü Aug 30 '18 at 08:19

1 Answers1

1

Based on our conversation with you in the comment section, I believe the problem lies in the method that you use→

doc.Load(new XmlTextReader(FileName))

Please be aware that XmlTextReader implements IDisposable interface and it uses Stream to read the file you supply in the method by Filename. Since you did not properly dispose new XmlTextReader(FileName) the file stream is still open and it is only natural that you get an exception that the file is being used by another application. I suggest that you use the using like follows, because it will properly dispose of XmlTextReader

using (var textReader = new XmlTextReader(FILENAME)){
    XDocument doc = XDocument.Load(textReader);
    //DO STUFF
}
Hasan Emrah Süngü
  • 3,488
  • 1
  • 15
  • 33