Vb.Net Xml文档格式化

最近在处理Webservice文档的时候,因为是未格式化的,需要处理,所以有了以下代码。

 

#Region "Xml字符串转换成格式化的XML文件"

    
    txt_Result.Text = StrToXml(txt_Result.Text)

    Public Function StrToXml(ByVal strTmp As String) As String

        XmlDocument可以从文件加载,也可以从字符串、数据流加载

        Dim Doc As New XmlDocument
        Dim strPath As String

        Dim MStream As New MemoryStream(1024)
        Dim XmlWriter As New XmlTextWriter(MStream, Nothing)
        Dim encoding As Encoding

        strPath = System.Windows.Forms.Application.StartupPath & "\XmlTemp.xml"

        从字符串读取
        Doc.LoadXml(strTmp)
        Doc.Save(strPath)

        XmlWriter.Formatting = Formatting.Indented

        Doc.Load(strPath)
        Doc.WriteTo(XmlWriter)
        XmlWriter.Flush()
        XmlWriter.Close()
        encoding = System.Text.Encoding.GetEncoding("utf-8")
        strTmp = encoding.GetString(MStream.ToArray())
        MStream.Close()
        StrToXml = strTmp

    End Function

#End Region

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。