C#操作XML

参考文章:http://msdn.microsoft.com/zh-cn/library/system.xml%28v=vs.100%29.aspx

创建xml 方法一

XmlDocument xmlDocument = new XmlDocument();

//在XML的文档的最头部加入XML的声明段落 
XmlNode xmlnode = xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmlDocument.AppendChild(xmlnode);

//创建根节点
XmlElement root = xmlDocument.CreateElement("root");
root.InnerText = "root";
xmlDocument.AppendChild(root);

xmlDocument.Save(@"C:\Users\lee\Desktop\abc.xml");

创建xml 方法二

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?><root id=\"root\"><list num=\"0\"><name type=\"string\">minglecun</name><age type=\"number\">55</age></list><list num=\"1\"><name type=\"string\">huluwa</name><age type=\"number\">1</age></list></root>");

xmlDocument.Save(@"C:\Users\lee\Desktop\abc.xml");

读取xml

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"C:\Users\lee\Desktop\abc.xml");

XmlNode xmlNode = xmlDocument.SelectSingleNode("//root//list");
Response.Write("<br/>//root//list  "+xmlNode.InnerText);
XmlNodeList xmlNodeList = xmlDocument.SelectSingleNode("//root").ChildNodes;
foreach (XmlNode node in xmlNodeList)
{
    Response.Write("<br/>//root 's childNodes  " + node.InnerXml);
}

你可能感兴趣的:(xml)