1、创建XML同时写入文件
2、创建XML同时写入内存
// 在内存中创建xml到字符串
另一种方法:
XmlDocument xmldoc = new XmlDocument();
XmlNode xRoot = xmldoc.CreateNode(XmlNodeType.Element, "root", "");
xmldoc.AppendChild(xRoot);
MemoryStream ms = new MemoryStream();
XmlTextWriter tw = new XmlTextWriter(ms, Encoding.Unicode);
tw.Formatting = Formatting.Indented;
tw.Indentation = 4;
xmldoc.Save(tw);
byte[] ary = ms.ToArray();
string s = Encoding.UTF8.GetString(ary);
// Response.Write(s); //输出看是正常的
xmldoc.LoadXml(s);
前面多了一个特殊字符,所以最后一行会出错
s = s.Substring(1, s.Length - 1);
3、从文件中读取XML
4、从内存中读取XML
// 从内存中读取xml字符串
问题及解决办法: