Q:

How to write to xml elements temporarily and clear the elements value that are written as soon as method is executed?

I have one xml template that looks like this:  
<?xml version="1.0" encoding="utf-8" ?>
 <EmailTemplate>
   <subject></subject>
   <displayName></displayName>
   <Message1>
   </Message1>
   <Copyright></Copyright>
 </EmailTemplate>

I am using LINQ to write values to the elements when the method is executing. after i write the values i use xslt transformation and get the html output in the same method. Everything works fine. but what i want is i want this xml to look like above. I mean the elements shouldn't contain any value after the method is executed successfully. At the moment as soon as the method is executed the xml contains values. My code for writing to xml looks like this:
        var xmlElement = XElement.Load(@"myxmlfile.xml");

        var element3 = xmlElement.Elements("subject").Single();
        element3.Value = subject;

        var element4 = xmlElement.Elements("displayName").Single();
        element4.Value = displayName;

        xmlElement.Save(@"myxmlfile.xml");

Note: if i don't include the last line (xmlelement.save...) during the transformation it doesn't pickup the values. Any help and suggestion most welcome.

A:

Remove the call to Save.  Just edit the XML in memory.  The Save method is used to modify the XML file on disk.  If you don't want that, then don't call the Save method.
If you need a fresh copy of the XML every time you need to create HTML, then I would suggest loading the string contents of the XML file in memory so you don't have to read from the disk every time, and using a StringReader to create the XML Document.
Based on the code you've provided, it looks like you wouldn't need to load a fresh XML document every time because you would just overwrite the existing values in memory with the new ones.

