xml实例:在C#.net中操作XML实例来源: 发布时间:星期四, 2009年2月12日 浏览:84次 评论:0
在C#.net中如何操作XML 需要添加命名空间: using.Xml; 定义几个公共对象: XmlDocumentxmldoc; XmlNodexmlnode; XmlElementxmlelem; 1创建到服务器同名目录下xml文件: 思路方法: xmldoc=XmlDocument; //加入XML声明段落 xmlnode=xmldoc.CreateNode(XmlNodeType.XmlDeclaration,\"\",\"\"); xmldoc.AppendChild(xmlnode); //加入个根元素 xmlelem=xmldoc.CreateElement(\"\",\"Employees\",\"\"); xmldoc.AppendChild(xmlelem); //加入另外个元素 for(i=1;i<3;i) { XmlNoderoot=xmldoc.SelectSingleNode(\"Employees\");//查找<Employees> XmlElementxe1=xmldoc.CreateElement(\"Node\");//创建个<Node>节点 xe1.SetAttribute(\"genre\",\"李赞红\");//设置该节点genre属性 xe1.SetAttribute(\"ISBN\",\"2-3631-4\");//设置该节点ISBN属性 XmlElementxesub1=xmldoc.CreateElement(\"title\"); xesub1.InnerText=\"CS从入门到精通\";//设置文本节点 xe1.AppendChild(xesub1);//添加到<Node>节点中 XmlElementxesub2=xmldoc.CreateElement(\"author\"); xesub2.InnerText=\"候捷\"; xe1.AppendChild(xesub2); XmlElementxesub3=xmldoc.CreateElement(\"price\"); xesub3.InnerText=\"58.3\"; xe1.AppendChild(xesub3); root.AppendChild(xe1);//添加到<Employees>节点中 } //保存创建好XML文档 xmldoc.Save(Server.MapPath(\"data.xml\")); ////////////////////////////////////////////////////////////////////////////////////// 结果:在同名目录下生成了名为data.xml文件内容如下 <?xmlversion=\"1.0\"?> <Employees> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> </Employees> 思路方法 2: XmlTextWriterxmlWriter; strFilename=Server.MapPath(\"data1.xml\"); xmlWriter=XmlTextWriter(strFilename,Encoding.Default);//创建个xml文档 xmlWriter.Formatting=Formatting.Indented; xmlWriter.WriteStartDocument; xmlWriter.WriteStartElement(\"Employees\"); xmlWriter.WriteStartElement(\"Node\"); xmlWriter.WriteAttributeString(\"genre\",\"李赞红\"); [Page] xmlWriter.WriteAttributeString(\"ISBN\",\"2-3631-4\"); xmlWriter.WriteStartElement(\"title\"); xmlWriter.WriteString(\"CS从入门到精通\"); xmlWriter.WriteEndElement; xmlWriter.WriteStartElement(\"author\"); xmlWriter.WriteString(\"候捷\"); xmlWriter.WriteEndElement; xmlWriter.WriteStartElement(\"price\"); xmlWriter.WriteString(\"58.3\"); xmlWriter.WriteEndElement; xmlWriter.WriteEndElement; xmlWriter.Close; ////////////////////////////////////////////////////////////////////////////////////// 结果: <?xmlversion=\"1.0\"encoding=\"gb2312\"?> <Employees> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> </Employees> 2添加个结点: XmlDocumentxmlDoc=XmlDocument; xmlDoc.Load(Server.MapPath(\"data.xml\")); XmlNoderoot=xmlDoc.SelectSingleNode(\"Employees\");//查找<Employees> XmlElementxe1=xmlDoc.CreateElement(\"Node\");//创建个<Node>节点 xe1.SetAttribute(\"genre\",\"张 3\");//设置该节点genre属性 xe1.SetAttribute(\"ISBN\",\"1-1111-1\");//设置该节点ISBN属性 XmlElementxesub1=xmlDoc.CreateElement(\"title\"); xesub1.InnerText=\"C#入门帮助\";//设置文本节点 xe1.AppendChild(xesub1);//添加到<Node>节点中 XmlElementxesub2=xmlDoc.CreateElement(\"author\"); xesub2.InnerText=\"高手\"; xe1.AppendChild(xesub2); XmlElementxesub3=xmlDoc.CreateElement(\"price\"); xesub3.InnerText=\"158.3\"; xe1.AppendChild(xesub3); root.AppendChild(xe1);//添加到<Employees>节点中 xmlDoc.Save(Server.MapPath(\"data.xml\")); ////////////////////////////////////////////////////////////////////////////////////// 结果:在xml原有内容里添加了个结点内容如下 <?xmlversion=\"1.0\"?> <Employees> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> <Nodegenre=\"张 3\"ISBN=\"1-1111-1\"> <title>C#入门帮助</title> <author>高手</author> <price>158.3</price> </Node> </Employees> 3修改结点值(属性和子结点): XmlDocumentxmlDoc=XmlDocument; [Page] xmlDoc.Load(Server.MapPath(\"data.xml\")); XmlNodeListnodeList=xmlDoc.SelectSingleNode(\"Employees\").ChildNodes;//获取Employees节点所有子节点 foreach(XmlNodexninnodeList)//遍历所有子节点 { XmlElementxe=(XmlElement)xn;//将子节点类型转换为XmlElement类型 (xe.GetAttribute(\"genre\")\"张 3\")//如果genre属性值为“张 3” { xe.SetAttribute(\"genre\",\"update张 3\");//则修改该属性为“update张 3” XmlNodeListnls=xe.ChildNodes;//继续获取xe子节点所有子节点 foreach(XmlNodexn1innls)//遍历 { XmlElementxe2=(XmlElement)xn1;//转换类型 (xe2.Name\"author\")//如果找到 { xe2.InnerText=\"亚胜\";//则修改 } } } } xmlDoc.Save(Server.MapPath(\"data.xml\"));//保存 ////////////////////////////////////////////////////////////////////////////////////// 结果:将原来所有结点信息都修改了xml内容如下 <?xmlversion=\"1.0\"?> <Employees> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> <Nodegenre=\"update张 3\"ISBN=\"1-1111-1\"> <title>C#入门帮助</title> <author>亚胜</author> <price>158.3</price> </Node> </Employees> 4修改结点(添加结点属性和添加结点自结点): XmlDocumentxmlDoc=XmlDocument; xmlDoc.Load(Server.MapPath(\"data.xml\")); XmlNodeListnodeList=xmlDoc.SelectSingleNode(\"Employees\").ChildNodes;//获取Employees节点所有子节点 foreach(XmlNodexninnodeList) { XmlElementxe=(XmlElement)xn; xe.SetAttribute(\"test\",\"111111\"); XmlElementxesub=xmlDoc.CreateElement(\"flag\"); xesub.InnerText=\"1\"; xe.AppendChild(xesub); } xmlDoc.Save(Server.MapPath(\"data.xml\")); ////////////////////////////////////////////////////////////////////////////////////// 结果:每个结点属性都添加了个子结点也添加了个内容如下 <?xmlversion=\"1.0\"?> <Employees> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"test=\"111111\"> <title>CS从入门到精通</title> <author>候捷</author> [Page] <price>58.3</price> <flag>1</flag> </Node> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"test=\"111111\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> <flag>1</flag> </Node> <Nodegenre=\"update张 3\"ISBN=\"1-1111-1\"test=\"111111\"> <title>C#入门帮助</title> <author>亚胜</author> <price>158.3</price> <flag>1</flag> </Node> </Employees> 5删除结点中某个属性: XmlDocumentxmlDoc=XmlDocument; xmlDoc.Load(Server.MapPath(\"data.xml\")); XmlNodeListxnl=xmlDoc.SelectSingleNode(\"Employees\").ChildNodes; foreach(XmlNodexninxnl) { XmlElementxe=(XmlElement)xn; xe.RemoveAttribute(\"genre\");//删除genre属性 XmlNodeListnls=xe.ChildNodes;//继续获取xe子节点所有子节点 foreach(XmlNodexn1innls)//遍历 { XmlElementxe2=(XmlElement)xn1;//转换类型 (xe2.Name\"flag\")//如果找到 { xe.RemoveChild(xe2);//则删除 } } } xmlDoc.Save(Server.MapPath(\"data.xml\")); //////////////////////////////////////////////////////////////////////////////////////] 结果:删除了结点个属性和结点个子结点内容如下 <?xmlversion=\"1.0\"?> <Employees> <NodeISBN=\"2-3631-4\"test=\"111111\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> <NodeISBN=\"2-3631-4\"test=\"111111\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> <NodeISBN=\"1-1111-1\"test=\"111111\"> <title>C#入门帮助</title> <author>亚胜</author> <price>158.3</price> </Node> </Employees> 6删除结点: XmlDocumentxmlDoc=XmlDocument; xmlDoc.Load(Server.MapPath(\"data.xml\")); XmlNoderoot=xmlDoc.SelectSingleNode(\"Employees\"); XmlNodeListxnl=xmlDoc.SelectSingleNode(\"Employees\").ChildNodes; for(i=0;i<xnl.Count;i) { XmlElementxe=(XmlElement)xnl.Item(i); (xe.GetAttribute(\"genre\")\"张 3\") { root.RemoveChild(xe); (i<xnl.Count)i=i-1; } } xmlDoc.Save(Server.MapPath(\"data.xml\")); [Page] //////////////////////////////////////////////////////////////////////////////////////] 结果:删除了符合条件所有结点原来内容: <?xmlversion=\"1.0\"?> <Employees> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> <Nodegenre=\"张 3\"ISBN=\"1-1111-1\"> <title>C#入门帮助</title> <author>高手</author> <price>158.3</price> </Node> <Nodegenre=\"张 3\"ISBN=\"1-1111-1\"> <title>C#入门帮助</title> <author>高手</author> <price>158.3</price> </Node> </Employees> 删除后内容: <?xmlversion=\"1.0\"?> <Employees> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> <Nodegenre=\"李赞红\"ISBN=\"2-3631-4\"> <title>CS从入门到精通</title> <author>候捷</author> <price>58.3</price> </Node> </Employees> 0
相关文章读者评论发表评论 |