将XMLstring格式化为可打印的XMLstring
我有这样的XMLstring:
<?xml version='1.0'?><response><error code='1'> Success</error></response>
一个元素与另一个元素之间没有线条,因此非常难以阅读。 我想要一个格式化上面的string的函数:
<?xml version='1.0'?> <response> <error code='1'> Success</error> </response>
没有诉诸手动写自己的格式函数,有没有.Net库或代码片断,我可以使用非手?
使用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) { } mStream.Close(); writer.Close(); return Result; }
你将不得不以某种方式parsing内容…我发现使用LINQ最简单的方法来做到这一点。 再次,这一切都取决于你的确切的情况。 这里有一个使用LINQ格式化inputXMLstring的工作示例。
string FormatXml(string xml) { try { XDocument doc = XDocument.Parse(xml); return doc.ToString(); } catch (Exception) { return xml; } }
[为简洁起见,使用语句是省略的]
来自克里斯托弗约翰森的这一个更好:
- 它也不需要XML文档头。
- 有更明确的例外
- 添加额外的行为选项:OmitXmlDeclaration = true,NewLineOnAttributes = true
-
较less的代码行
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(); }
检查以下链接: 如何漂亮地打印XML (不幸的是,链接现在返回404 :()
链接中的方法将XMLstring作为参数,并返回格式良好的(缩进的)XMLstring。
我只是从链接复制示例代码,使这个答案更全面和方便。
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; }
简单的解决scheme,为我工作:
XmlDocument xmlDoc = new XmlDocument(); StringWriter sw = new StringWriter(); xmlDoc.LoadXml(rawStringXML); xmlDoc.Save(sw); String formattedXml = sw.ToString();
.NET 2.0忽略名称parsing,并使用适当的资源处置,缩进,保留空白和自定义编码 :
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);
我试过了 :
internal static void IndentedNewWSDLString(string filePath) { var xml = File.ReadAllText(filePath); XDocument doc = XDocument.Parse(xml); File.WriteAllText(filePath, doc.ToString()); }
它按预期正常工作。
如果你加载的XMLDoc我敢肯定.ToString()函数具有这个重载。
但这是为了debugging? 这样发送的原因是占用较less的空间(即从XML中删除不必要的空格)。
使用UTF-8 XML声明可定制漂亮的XML输出
下面的类定义提供了一个简单的方法来将input的XMLstring转换为格式化的输出XML,其中xml声明为UTF-8。 它支持XmlWriterSettings类提供的所有configuration选项。
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; } } } } }
进一步改善的可能性: –
- 可以创build一个额外的方法
GetPrettyXml(XmlDocument doc, XmlWriterSettings settings)
,允许调用者自定义输出。 - 可以添加额外的方法
GetPrettyXml(String rawXml)
,该方法支持parsing原始文本,而不是让客户端使用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; }