C#XMLElement.OuterXML中的一行,而不是格式而不是、格式、XMLElement、OuterXML

2023-09-04 04:28:49 作者:放手也是种幸福

我想从一个WCF服务使用log4net的记录一些XML响应。

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

我希望XML文件的日志输出将在格式良好的XML。请求进来作为XMLELEMENT。

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

例如:

请求进来,如下:

<?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>

那么,如果我输出此值log4net的。

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>

我想它在正确格式化的log.txt文件显示,因为它进来到目前为止,我发现的唯一办法做到这一点是将其转换为像这样的的XElement:

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:删除的ToString(),因为OuterXML是一个字符串值

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

EDIT2:我回答我自己的问题:

edit2: I answered my own question:

所以,我做了一些调查研究,我想我错过了一张code中的文档。

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

http://msdn.microsoft.com/ EN-US /库/ system.xml.xmlnode.outerxml.aspx

我有它归结为:

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.

推荐答案

的XElement解析是最彻底的方法。您可以节省一两行有:

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

logger.Info(XElement.Parse(request.OuterXml).ToString());