Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# XMLElement.OuterXML in a single line rather than format

I am trying to log some XML responses from a WCF Service using log4net.

I want the output of the XML file to the log to be in properly formed XML. The request comes in as an XMLElement.

Example:

The request comes in as this:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationEvent xmlns="http://courts.wa.gov/INH_TV/ApplicationEvent.xsd">
  <Severity xmlns="">Information</Severity>
  <Application xmlns="">Application1</Application>
  <Category xmlns="">Timings</Category>
  <EventID xmlns="">1000</EventID>
  <DateTime xmlns="">2012-09-02T12:05:15.234Z</DateTime>
  <MachineName xmlns="">Server1</MachineName>
  <MessageID xmlns="">10000000-0000-0000-0000-000000000000</MessageID>
  <Program xmlns="">Progam1</Program>
  <Action xmlns="">Entry</Action>
  <UserID xmlns="">User1</UserID>
</ApplicationEvent>

Then if I output this value to log4net.

logger.Info(request.OuterXml);

I get the entire document logged in a single line like so:

<ApplicationEvent xmlns="http://courts.wa.gov/INH_TV/ApplicationEvent.xsd"><Severity xmlns="">Information</Severity><Application xmlns="">Application1</Application><Category xmlns="">Timings</Category><EventID xmlns="">1000</EventID><DateTime xmlns="">2012-09-02T12:05:15.234Z</DateTime><MachineName xmlns="">Server1</MachineName><MessageID xmlns="">10000000-0000-0000-0000-000000000000</MessageID><Program xmlns="">Progam1</Program><Action xmlns="">Entry</Action><UserID xmlns="">User1</UserID></ApplicationEvent>

I would like it to display in the log.txt file formatted correctly as it came in. So far the only way I have found to do this is to convert it to an XElement like so:

XmlDocument logXML = new XmlDocument();
logXML.AppendChild(logXML.ImportNode(request, true));
XElement logMe = XElement.Parse(logXML.InnerXml);
logger.Info(logMe.ToString());

This doesn't seem like good programming to me. I have been searching the documentation and I can't find a built-in way to output this correctly without converting it.

Is there an obvious, better way that I am just missing?

edit1: Removed ToString() since OuterXML is a String value.

edit2: I answered my own question:

So I did some more research, and I guess I missed a piece of code in the documentation.

http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.outerxml.aspx

I have it down to:

using (MemoryStream ms = new MemoryStream())
        {
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.Indent = true;
            using (XmlWriter xmlWriter = XmlWriter.Create(ms, xws))
            {
                request.WriteTo(xmlWriter);
            }
            ms.Position = 0; StreamReader sr = new StreamReader(ms);
            string s = sr.ReadToEnd(); // s will contain indented xml
            logger.Info(s);
        }

Which is a little more efficient than my current method despite being more verbose.

like image 225
noodles Avatar asked Oct 12 '11 17:10

noodles


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in coding language?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Why was C written?

The C language was actually created to move the UNIX kernel code from assembly to a higher level language, which would do the same tasks with fewer lines of code. Oracle database development started in 1977, and its code was rewritten from assembly to C in 1983. It became one of the most popular databases in the world.


1 Answers

XElement parse is the cleanest way. You can save a line or two with:

logger.Info(XElement.Parse(request.OuterXml).ToString()); 
like image 110
Joe Mancuso Avatar answered Oct 05 '22 22:10

Joe Mancuso