一.从String xml到XmlDocument的:
string xml = "<XML><Test>Hello World</Test></XML>" XmlDocument doc = new XmlDocument(); xml.loadXml(xml);
二.将XmlDocument内容 转换成String xml
//(1)如果只要求字符串的话用xmlDocument 的 OuterXml 方法就可以实现: doc.OutXml; //(2)转字节流的方式 //这种方式可以把 xmlDocument 内容变成比较符合标准格式的 xml 字符串,例如: //<?xml version="1.0" encoding="utf-8"?> //<XML> // <Test>Hello World</Test> //</XML> // xmlDocument to string public static string xmlDocument2String(XmlDataDocument doc) { MemoryStream stream = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8); writer.Formatting = Formatting.Indented; doc.Save(writer); StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8); stream.Position = 0; string xmlstring = sr.ReadToEnd(); sr.Close(); stream.Close(); return xmlstring; } //(3)先写到本地文件,再字符串形式流读取: public string XmltoString(string path) { string strXML = ""; string strLine = ""; StreamReader objReader = new StreamReader(path); // read line while ((strLine = objReader.ReadLine()) != null) { strXML += strLine; } objReader.Close(); return strXML; }