0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
nzdev
  • 324
  • 1
  • 9
  • 24
  • So basically you want to use the XML file as a template for generating HTML via XLST? Can you post the code where you do the transformation? Just thinking off the top of my head (I'd have to check MSDN) but maybe you could load the XML into a stream and run the transformation that way, without touching the template XML once you've loaded the values? – Tim Mar 24 '13 at 03:55
  • Take a look at this answer - it may help you: http://stackoverflow.com/a/2389628/745969 – Tim Mar 24 '13 at 03:59
  • Yep i want to use XML file as a template for generating HTML via XSLT. The transformation part is fine but saving the values to xml temporarily is what is need to do. – nzdev Mar 24 '13 at 04:03
  • @Tim this is how i have done the transformation http://msdn.microsoft.com/en-us/library/ms163438.aspx – nzdev Mar 24 '13 at 04:03
  • See @Dan's answer below or the link I posted above. You should be able to do this in memory without altering the original document. – Tim Mar 24 '13 at 04:03

1 Answers1

0

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.

Dan
  • 9,717
  • 4
  • 47
  • 65
  • hi thanks for your answer i was keeping that method as an alternative. My xml is too big so i am worried about editing in memory. Whats your suggestion won't that cause any performance issue? – nzdev Mar 24 '13 at 04:08
  • How big are we talking? I think keeping a single instance of the XML in memory, and editing it in memory without saving it to disk would be the best performing method to solve your problem. – Dan Mar 24 '13 at 04:10
  • Its about 80 lines. So i am worried. Anyway thanks for your help i will try that. – nzdev Mar 24 '13 at 04:13
  • Well, unless your computer is from 1985, you should probably be okay with that. :) – Dan Mar 24 '13 at 04:14