C#XmlDocument输出字符串

时间:2024-01-24 18:44:39

XmlDocument xmlDoc = new XmlDocument();

//假定该xmlDoc已经有内容;

MemoryStream streamXml = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(streamXml, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
xmlDoc.Save(writer);

StreamReader reader = new StreamReader(streamXml, Encoding.UTF8);
streamXml.Position = 0;
String content = reader.ReadToEnd();
reader.Close();
streamXml.Close();

Console.WriteLine(content);