如何将XMLDocument转化成String

曾经一个程序要求将xml转成string,当时就脑袋大了,谁也不知道怎么弄,于是搜了一下,将大家比较推崇的一片文章转出来,据说是一老外写的

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(@”C:\test.xml”);
richTextBox1.Text = Class1.xml2string(xmlDoc);

public static string xml2string(XmlDocument xmlDoc)
{
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, null);
writer.Formatting = Formatting.Indented;
xmlDoc.Save(writer);
StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
stream.Position = 0;
string xmlString = sr.ReadToEnd();
sr.Close();
stream.Close();
return xmlString;
}

很好用,大家414 

    原文作者:冰Ivan
    原文地址: https://blog.csdn.net/icysonyk/article/details/8850498
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞