Solved

Write XML Log

Posted on 2006-10-31
5
585 Views
Last Modified: 2008-01-09
Hi,

I want to write a log in xml format...a simple log...just recording date and time and some context...
This is what I do...

WriteLog("Start Program...");
WriteLog("Testing 1...");
WriteLog("Testing 2...");

public void WriteLog(string logstr)
{
            XmlTextWriter   xmlWriter = new XmlTextWriter("HttpListenerLog.xml", System.Text.Encoding.UTF8);
            DateTime        dt = DateTime.Now;

            xmlWriter.Formatting = Formatting.Indented;
            xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
            xmlWriter.WriteStartElement("MSG");
            xmlWriter.WriteAttributeString("Time", dt.ToString());
            xmlWriter.WriteAttributeString("Msg", logstr);
           
            xmlWriter.Close();
}

Here is what I wanna see...
<?xml version='1.0' encoding='UTF-8'?>
<MSG>
<INFO Time="2006/11/1 AM 11:08:48" Msg="Start Program..." />
<INFO Time="2006/11/1 AM 11:08:50" Msg="Testing 1..."/>
</MSG>

Something like this.  How can I modify my code to do this?
0
Comment
Question by:xenia27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:NewMom2Brandon
ID: 17862134
public void WriteLog(string logstr)
{
        DateTime        dt = DateTime.Now;

        XmlTextWriter   xmlWriter = new XmlTextWriter("HttpListenerLog.xml", System.Text.Encoding.UTF8);
       
         //This is the start of the document      
            xmlWriter.Formatting = Formatting.Indented;
            xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");

            xmlWriter.WriteStartElement("MSG");
            xmlWriter.WriteStartElement("INFO");
            xmlWriter.WriteAttributeString("Time", dt.ToString());
            xmlWriter.WriteAttributeString("Msg", logstr);
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndElement();

         //this is the end of the document
         xmlWriter.WriteEndDocument();              
         xmlWriter.Flush();
         xmlWriter.Close();

}
0
 
LVL 2

Expert Comment

by:NewMom2Brandon
ID: 17862304

you will need to do a foreach inside your function to get all of the INFO fields.

public void WriteLog(string logstr)
{
        DateTime        dt = DateTime.Now;

        XmlTextWriter   xmlWriter = new XmlTextWriter("HttpListenerLog.xml", System.Text.Encoding.UTF8);
       
         //This is the start of the document      
            xmlWriter.Formatting = Formatting.Indented;
            xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");

            xmlWriter.WriteStartElement("MSG");
****Do a foreach here
            xmlWriter.WriteStartElement("INFO");
            xmlWriter.WriteAttributeString("Time", dt.ToString());
            xmlWriter.WriteAttributeString("Msg", logstr);
            xmlWriter.WriteEndElement();
*** end foreach
            xmlWriter.WriteEndElement();

         //this is the end of the document
         xmlWriter.WriteEndDocument();              
         xmlWriter.Flush();
         xmlWriter.Close();

}
0
 

Author Comment

by:xenia27
ID: 17863618
If I have something existed in HttpListenerLog.xml file and I would like to keep it in my file and add new context, how can I do so?
Thanks for your help~
0
 
LVL 2

Accepted Solution

by:
NewMom2Brandon earned 500 total points
ID: 17866597
You can do several different things you can so

1. you can move all of your data into a hashtable, then add what you need then have it write back out.

2. You can add your data into a dataset. then add to the dataset and then write it backout

3. http://www.c-sharpcorner.com/xml/InsertDatatoXMLDocument.asp

4. You can use XPath

5. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconinsertingnodesintoxmldocument.asp

6.

XmlDocument doc = new XmlDocument();
FileStream myFile = new FileStream(@"C:\blabla.xml", FileMode.Open);
doc.Load(myFile);
XmlNode root = doc.FirstChild;
//create new node
XmlElement elem = doc.CreateElement("INFO");
elem.InnerText = "this is my text to add";
//add node to the document
root.InsertAfter(elem, root.FirstChild);
0
 

Author Comment

by:xenia27
ID: 17878470
Thanks for your help~ ^^
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Amazon S3 .Net error 5 78
Stop Git from being my repository 1 49
C# rename a class and update all references efficiently 6 53
VS C# how to design libary of functions 7 33
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question