Link to home
Start Free TrialLog in
Avatar of realcoding
realcodingFlag for United States of America

asked on

how to append a xml file in c# if exists, or make new if not?

i am adding tracing for audit purposes of a simple process i have built as an .exe and set in the scheduler to run every 10 minutes. i want to have the application output the results into an xml file.

if the file exists then open and append data to it, if it does not exist i want to create a new xml file that will be persisted and used on next run.

here is my code now, what do i need to add, how do i open the xml file (on c:/file.xml) and use it to append nodes to?

also i think my code is like a newbs, so some help in showing me the correct streamlined way would help me learn best practice with C#/ XML

string err = "";
        XmlDocument doc = new XmlDocument();// Create the XML Declaration, and append it to XML document

        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
        doc.AppendChild(dec);// Create the root element

        XmlElement root = doc.CreateElement("STATS");
        doc.AppendChild(root);

        // Create URL's            
        XmlElement urlNode = doc.CreateElement("keepalive");
        urlNode.SetAttribute("runTime", DateTime.Now.ToString());

        try
        {
            WebProxy wp = new WebProxy("http://proxy.ml.com:8083/");
            WebClient w = new WebClient();
            w.Proxy = wp;

            if (w.DownloadString("http://wwww.idealcoding.com") != "")
                urlNode.SetAttribute("result", "UP");
            else
                urlNode.SetAttribute("result", "DOWN");
        }
        catch (Exception ex)
        {
            err = ex.Message;
            urlNode.SetAttribute("result", "DOWN");
        }
        finally {
            root.AppendChild(urlNode);
            doc.Save("c:/keepAlive.xml");
        }

Open in new window



this is my XML file:
 
<STATS>
  <keepalive runTime="11/22/2010 9:40:43 AM" result="UP" /> 
 </STATS>

Open in new window

Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

What is it you want to add to the file, a new <STATS /> everytime it runs or a new <keepalive /> adding to STATS?
Avatar of realcoding

ASKER

i want to just add keepalive nodes, such that each run of the app will log the time it ran and result.
if the file exists i want to load it into the xml object and add a keepalive node with the current runs info and so on... if the xml file does not exist then do as i do here and create a new one.

but as it stands every run of the app creates and over-writes the xml file leaving only one node , not very useful.
SOLUTION
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also to handle when this file gets very large I would write to a new write each hour / day. You could create folders for further organization. File would be suffixed with Date & Time. Folders would be suffixed with Date.

DaTribe
why would you use a serializable object? whats the gain?
The gain for me would be simply manipulating this object easily with c# and then in the end simply turning it back to XML. Avoid the hassles with XML plus another huge bonus would be I could add or remove properties from the Audit class and deserialization would still be successful and as the file contents are over-written when saving then there would be no problem saving the new structure.

I save myself development time and hassles. More maintainable solution. In this instance as its every 10 minutes mission critical performance is not required - still some of the suggestions I provided work around that.

DaTribe