Cita de: FJDA en 11 Abril 2019, 20:38 PM
Me ha parecido muy curioso así que posteo.
Un código de uso genérico que te puede servir. Lo he extraido de mi framework comercial: DevCase for .NET Framework
Código (vbnet) [Seleccionar]
' Namespace DevCase.Core.Xml.Tools
Partial Public NotInheritable Class XmlUtil
#Region " Public Methods "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Beautifies the contents of an Xml document.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim enc As Encoding = Encoding.Default
'''
''' Dim unformattedXmlDocument As String =
''' File.ReadAllText("C:\Unformatted Document.xml", enc)
'''
''' Dim formattedXmlDocument As String =
''' XmlBeautifier(xmlText:=unformattedXmlDocument,
''' indentChars:=New String(" "c, 2),
''' indentOnAttributes:=True,
''' enc:=enc)
'''
''' File.WriteAllText("C:\Formatted Document.xml", formattedXmlDocument, enc)
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="xmlText">
''' The Xml text content.
''' It can be an entire document or a fragment.
''' </param>
'''
''' <param name="indentChars">
''' The string that is used to indent the Xml.
''' <para></para>
''' Default value is <see cref="ControlChars.Tab"/>
''' </param>
'''
''' <param name="indentOnAttributes">
''' If set to <see langword="True"/>, attributes will be separated by newlines.
''' </param>
'''
''' <param name="enc">
''' The Xml text encoding to use.
''' <para></para>
''' Default value is <see cref="Encoding.Default"/>.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' The beautified Xml text.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
''' <exception cref="ArgumentNullException">
''' xmlText
''' </exception>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Function XmlBeautifier(ByVal xmlText As String,
Optional ByVal indentChars As String = "",
Optional ByVal indentOnAttributes As Boolean = False,
Optional ByVal enc As Encoding = Nothing) As String
If String.IsNullOrEmpty(xmlText) OrElse String.IsNullOrWhiteSpace(xmlText) Then
Throw New ArgumentNullException(paramName:=NameOf(xmlText))
Else
Dim xmlDoc As New XmlDocument
xmlDoc.LoadXml(xmlText)
Return XmlBeautifier(xmlDoc, indentChars, indentOnAttributes, enc)
End If
End Function
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Beautifies the contents of an Xml document.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim enc As Encoding = Encoding.Default
'''
''' Dim xmlDoc As New XmlDocument
''' xmlDoc.LoadXml("xmlText")
'''
''' Dim formattedXmlDocument As String =
''' XmlBeautifier(xmlDoc:=xmlDoc,
''' indentChars:=New String(" "c, 2),
''' indentOnAttributes:=True,
''' enc:=enc)
'''
''' File.WriteAllText("C:\Formatted Document.xml", formattedXmlDocument, enc)
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="xmlDoc">
''' The Xml document.
''' It can be an entire document or a fragment.
''' </param>
'''
''' <param name="indentChars">
''' The string that is used to indent the Xml.
''' <para></para>
''' Default value is <see cref="ControlChars.Tab"/>
''' </param>
'''
''' <param name="indentOnAttributes">
''' If set to <see langword="True"/>, attributes will be separated by newlines.
''' </param>
'''
''' <param name="enc">
''' The Xml text encoding to use.
''' <para></para>
''' Default value is <see cref="Encoding.Default"/>.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' The beautified Xml text.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
''' <exception cref="ArgumentNullException">
''' xmlText
''' </exception>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Function XmlBeautifier(ByVal xmlDoc As XmlDocument,
Optional ByVal indentChars As String = "",
Optional ByVal indentOnAttributes As Boolean = False,
Optional ByVal enc As Encoding = Nothing) As String
Dim sb As New Global.System.Text.StringBuilder(capacity:=4096)
Dim settings As New XmlWriterSettings With {
.Indent = True,
.CheckCharacters = True,
.OmitXmlDeclaration = False,
.ConformanceLevel = ConformanceLevel.Auto,
.NamespaceHandling = NamespaceHandling.Default,
.NewLineHandling = NewLineHandling.Replace,
.NewLineChars = ControlChars.NewLine,
.NewLineOnAttributes = indentOnAttributes,
.IndentChars = If(indentChars, ControlChars.Tab),
.Encoding = If(enc, Encoding.Default),
.CloseOutput = True
}
Using writer As XmlWriter = XmlWriter.Create(sb, settings)
xmlDoc.WriteContentTo(writer)
writer.Flush()
End Using
Return sb.ToString()
End Function
#End Region
End Class
' End Namespace
Saludos.