C#文本之XML

 

 

格式化XML

 public static string FormatXML(string XMLstring)
        {
           //校验是否是XML报文
            if (!XMLstring.Contains("<?xml version")) return XMLstring;           
            XmlDocument document = new XmlDocument();
            document.LoadXml(XMLstring);          

            MemoryStream memoryStream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(memoryStream, null)
            {
                Formatting = Formatting.Indented//缩进
            };
            document.Save(writer);
            StreamReader streamReader = new StreamReader(memoryStream);
            memoryStream.Position = 0;
            string xmlString = streamReader.ReadToEnd();
            streamReader.Close();
            memoryStream.Close();
            return xmlString;
        }

 

posted @ 2019-04-14 13:43  quanzhan  阅读(163)  评论(0编辑  收藏  举报