曾经一个程序要求将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