格式化XML字符串以打印友好的XML字符串

时间:2009-07-14 06:15:38

标签: c# xml formatting

我有一个XML字符串:

<?xml version='1.0'?><response><error code='1'> Success</error></response>

一个元素与另一个元素之间没有线条,因此很难阅读。我想要一个格式化上述字符串的函数:

<?xml version='1.0'?>
<response>
<error code='1'> Success</error>
</response> 

不依靠自己手动编写格式函数,是否有任何我可以随意使用的.Net库或代码片段?

11 个答案:

答案 0 :(得分:280)

你必须以某种方式解析内容......我发现使用LINQ是最简单的方法。同样,这完全取决于您的具体情况。这是一个使用LINQ格式化输入XML字符串的工作示例。

string FormatXml(string xml)
{
     try
     {
         XDocument doc = XDocument.Parse(xml);
         return doc.ToString();
     }
     catch (Exception)
     {
         // Handle and throw if fatal exception here; don't just ignore them
         return xml;
     }
 }

[为简洁起见,省略了使用陈述]

答案 1 :(得分:160)

使用XmlTextWriter ...

public static string PrintXML(string xml)
{
    string result = "";

    MemoryStream mStream = new MemoryStream();
    XmlTextWriter writer = new XmlTextWriter(mStream, Encoding.Unicode);
    XmlDocument document = new XmlDocument();

    try
    {
        // Load the XmlDocument with the XML.
        document.LoadXml(xml);

        writer.Formatting = Formatting.Indented;

        // Write the XML into a formatting XmlTextWriter
        document.WriteContentTo(writer);
        writer.Flush();
        mStream.Flush();

        // Have to rewind the MemoryStream in order to read
        // its contents.
        mStream.Position = 0;

        // Read MemoryStream contents into a StreamReader.
        StreamReader sReader = new StreamReader(mStream);

        // Extract the text from the StreamReader.
        string formattedXml = sReader.ReadToEnd();

        result = formattedXml;
    }
    catch (XmlException)
    {
        // Handle the exception
    }

    mStream.Close();
    writer.Close();

    return result;
}

答案 2 :(得分:48)

This one, from kristopherjohnson更好了:

  1. 它也不需要XML文档标题。
  2. 有更明确的例外
  3. 添加额外的行为选项:OmitXmlDeclaration = true,NewLineOnAttributes = true
  4. 减少代码行数

    static string PrettyXml(string xml)
    {
        var stringBuilder = new StringBuilder();
    
        var element = XElement.Parse(xml);
    
        var settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        settings.Indent = true;
        settings.NewLineOnAttributes = true;
    
        using (var xmlWriter = XmlWriter.Create(stringBuilder, settings))
        {
            element.Save(xmlWriter);
        }
    
        return stringBuilder.ToString();
    }
    

答案 3 :(得分:8)

对我有用的简单解决方案:

        XmlDocument xmlDoc = new XmlDocument();
        StringWriter sw = new StringWriter();
        xmlDoc.LoadXml(rawStringXML);
        xmlDoc.Save(sw);
        String formattedXml = sw.ToString();

答案 4 :(得分:6)

检查以下链接: How to pretty-print XML (不幸的是,该链接现在返回404 :()

链接中的方法将XML字符串作为参数,并返回格式正确(缩进)的XML字符串。

我刚从链接中复制了示例代码,使这个答案更加全面和方便。

public static String PrettyPrint(String XML)
{
    String Result = "";

    MemoryStream MS = new MemoryStream();
    XmlTextWriter W = new XmlTextWriter(MS, Encoding.Unicode);
    XmlDocument D   = new XmlDocument();

    try
    {
        // Load the XmlDocument with the XML.
        D.LoadXml(XML);

        W.Formatting = Formatting.Indented;

        // Write the XML into a formatting XmlTextWriter
        D.WriteContentTo(W);
        W.Flush();
        MS.Flush();

        // Have to rewind the MemoryStream in order to read
        // its contents.
        MS.Position = 0;

        // Read MemoryStream contents into a StreamReader.
        StreamReader SR = new StreamReader(MS);

        // Extract the text from the StreamReader.
        String FormattedXML = SR.ReadToEnd();

        Result = FormattedXML;
    }
    catch (XmlException)
    {
    }

    MS.Close();
    W.Close();

    return Result;
}

答案 5 :(得分:3)

我试过了:

internal static void IndentedNewWSDLString(string filePath)
{
    var xml = File.ReadAllText(filePath);
    XDocument doc = XDocument.Parse(xml);
    File.WriteAllText(filePath, doc.ToString());
}

它正如预期的那样正常工作。

答案 6 :(得分:2)

.NET 2.0忽略名称解析,并使用适当的资源处理,缩进,保留空白和自定义编码

public static string Beautify(System.Xml.XmlDocument doc)
{
    string strRetValue = null;
    System.Text.Encoding enc = System.Text.Encoding.UTF8;
    // enc = new System.Text.UTF8Encoding(false);

    System.Xml.XmlWriterSettings xmlWriterSettings = new System.Xml.XmlWriterSettings();
    xmlWriterSettings.Encoding = enc;
    xmlWriterSettings.Indent = true;
    xmlWriterSettings.IndentChars = "    ";
    xmlWriterSettings.NewLineChars = "\r\n";
    xmlWriterSettings.NewLineHandling = System.Xml.NewLineHandling.Replace;
    //xmlWriterSettings.OmitXmlDeclaration = true;
    xmlWriterSettings.ConformanceLevel = System.Xml.ConformanceLevel.Document;


    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
        using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(ms, xmlWriterSettings))
        {
            doc.Save(writer);
            writer.Flush();
            ms.Flush();

            writer.Close();
        } // End Using writer

        ms.Position = 0;
        using (System.IO.StreamReader sr = new System.IO.StreamReader(ms, enc))
        {
            // Extract the text from the StreamReader.
            strRetValue = sr.ReadToEnd();

            sr.Close();
        } // End Using sr

        ms.Close();
    } // End Using ms


    /*
    System.Text.StringBuilder sb = new System.Text.StringBuilder(); // Always yields UTF-16, no matter the set encoding
    using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sb, settings))
    {
        doc.Save(writer);
        writer.Close();
    } // End Using writer
    strRetValue = sb.ToString();
    sb.Length = 0;
    sb = null;
    */

    xmlWriterSettings = null;
    return strRetValue;
} // End Function Beautify

用法:

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.XmlResolver = null;
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load("C:\Test.svg");
string SVG = Beautify(xmlDoc);

答案 7 :(得分:1)

检查以下链接:Format an XML file so it looks nice in C#

distnct_sessions|  event_type   
----------------+---------------
2.0             |page_view      
...

答案 8 :(得分:0)

如果加载XMLDoc我很确定.ToString()函数会为此加载。

但这是用于调试吗?这样发送它的原因是占用更少的空间(即从XML中删除不必要的空格)。

答案 9 :(得分:0)

使用UTF-8 XML声明的可自定义的Pretty XML输出

以下类定义提供了一种将输入XML字符串转换为格式化输出XML的简单方法,其中xml声明为UTF-8。它支持XmlWriterSettings类提供的所有配置选项。

using System;
using System.Text;
using System.Xml;
using System.IO;

namespace CJBS.Demo
{
    /// <summary>
    /// Supports formatting for XML in a format that is easily human-readable.
    /// </summary>
    public static class PrettyXmlFormatter
    {

        /// <summary>
        /// Generates formatted UTF-8 XML for the content in the <paramref name="doc"/>
        /// </summary>
        /// <param name="doc">XmlDocument for which content will be returned as a formatted string</param>
        /// <returns>Formatted (indented) XML string</returns>
        public static string GetPrettyXml(XmlDocument doc)
        {
            // Configure how XML is to be formatted
            XmlWriterSettings settings = new XmlWriterSettings 
            {
                Indent = true
                , IndentChars = "  "
                , NewLineChars = System.Environment.NewLine
                , NewLineHandling = NewLineHandling.Replace
                //,NewLineOnAttributes = true
                //,OmitXmlDeclaration = false
            };

            // Use wrapper class that supports UTF-8 encoding
            StringWriterWithEncoding sw = new StringWriterWithEncoding(Encoding.UTF8);

            // Output formatted XML to StringWriter
            using (XmlWriter writer = XmlWriter.Create(sw, settings))
            {
                doc.Save(writer);
            }

            // Get formatted text from writer
            return sw.ToString();
        }



        /// <summary>
        /// Wrapper class around <see cref="StringWriter"/> that supports encoding.
        /// Attribution: http://stackoverflow.com/a/427737/3063884
        /// </summary>
        private sealed class StringWriterWithEncoding : StringWriter
        {
            private readonly Encoding encoding;

            /// <summary>
            /// Creates a new <see cref="PrettyXmlFormatter"/> with the specified encoding
            /// </summary>
            /// <param name="encoding"></param>
            public StringWriterWithEncoding(Encoding encoding)
            {
                this.encoding = encoding;
            }

            /// <summary>
            /// Encoding to use when dealing with text
            /// </summary>
            public override Encoding Encoding
            {
                get { return encoding; }
            }
        }
    }
}

进一步改善的可能性: -

  • 可以创建一个允许调用者自定义输出的附加方法GetPrettyXml(XmlDocument doc, XmlWriterSettings settings)
  • 可以添加一个支持解析原始文本的附加方法GetPrettyXml(String rawXml),而不是让客户端使用XmlDocument。就我而言,我需要使用XmlDocument操作XML,因此我没有添加它。

用法:

String myFormattedXml = null;
XmlDocument doc = new XmlDocument();
try
{
    doc.LoadXml(myRawXmlString);
    myFormattedXml = PrettyXmlFormatter.GetPrettyXml(doc);
}
catch(XmlException ex)
{
    // Failed to parse XML -- use original XML as formatted XML
    myFormattedXml = myRawXmlString;
}

答案 10 :(得分:0)

可以通过带有 XmlWriter.WriteNode(XmlReader, true)流转换来漂亮地打印 XML 字符串。这个方法

<块引用>

复制从读取器到写入器的所有内容,并将读取器移动到下一个兄弟节点的开头。

定义以下扩展方法:

public static class XmlExtensions
{
    public static string FormatXml(this string xml, bool indent = true, bool newLineOnAttributes = false, string indentChars = "  ", ConformanceLevel conformanceLevel = ConformanceLevel.Document) => 
        xml.FormatXml( new XmlWriterSettings { Indent = indent, NewLineOnAttributes = newLineOnAttributes, IndentChars = indentChars, ConformanceLevel = conformanceLevel });

    public static string FormatXml(this string xml, XmlWriterSettings settings)
    {
        using (var textReader = new StringReader(xml))
        using (var xmlReader = XmlReader.Create(textReader, new XmlReaderSettings { ConformanceLevel = settings.ConformanceLevel } ))
        using (var textWriter = new StringWriter())
        {
            using (var xmlWriter = XmlWriter.Create(textWriter, settings))
                xmlWriter.WriteNode(xmlReader, true);
            return textWriter.ToString();
        }
    }
}

现在您将能够:

var inXml = @"<?xml version='1.0'?><response><error code='1'> Success</error></response>";
var newXml = inXml.FormatXml(indentChars : "", newLineOnAttributes : false); // Or true, if you prefer
Console.WriteLine(newXml);

打印什么

<?xml version='1.0'?>
<response>
<error code="1"> Success</error>
</response>

注意事项:

  • 其他答案将 XML 加载到某个 Document Object Model 中,例如 XmlDocumentXDocument/XElement,然后在启用缩进的情况下重新序列化 DOM。

    这种流式解决方案完全避免了 DOM 增加的内存开销。

  • 在您的问题中,您没有为嵌套的 <error code='1'> Success</error> 节点添加任何缩进,因此我设置了 indentChars : ""。通常,每级嵌套缩进两个空格是惯例。

  • 如果当前是单引号,属性分隔符将无条件转换为双引号。 (我相信其他答案也是如此。)

  • 传递 conformanceLevel : ConformanceLevel.Fragment 允许格式化包含 XML 片段序列的字符串。

  • ConformanceLevel.Fragment 外,输入的 XML 字符串必须格式正确。如果不是,XmlReader 将抛出异常。

演示小提琴here