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