XML操作集合
来源:互联网 发布:mysql位数 编辑:程序博客网 时间:2024/06/03 21:16
1 创建XML文档
使用XmlDocument类型创建XML文档
XmlDocument doc = new XmlDocument();
2 将XML文档读入DOM
2.1 可使用XmlDoucment.Load方法的不同重载将文档置入内存
2.2 可使用XmlDoucment.LoadXml方法从字符串中读取XML
举例:
XmlDoucment doc = new XmlDoucment();
doc.LoadXml("<book genre = 'novel' ISBN = '1-861001-57-3'>" + "<title> Pride And Prejudice</title>" + "</book>");
doc.Save("data.xmls");
从读取器中加载数据
XmlDoucment doc = new XmlDoucment();
XmlReaderSettings setting = new XmlReaderSettings();
setting.IgnoreWhiteSpace = true ;
XmlReader reader = XmlReader.Create("bookstore.xml", setting);
reader.MoverTocontent();
reader.Read();
reader.Skip();
reader.Skip();
doc.Load(reader);
3 检索单个属性节点
XmlDoucment doc = new xmlDocument();
doc.LoadXml("<book genre = 'novel' ISBN = '1-861001-57-3'>" + "<title> Pride And Prejudice</title>" + "</book>");
XmlElement root = doc.DocumetElement;
XmlAttribute attr = root.GetAttributeNode("ISBN");
String attriValue = attr.InnerXml;
4创建并添加注释
XmlDoucment doc = new xmlDocument();
doc.LoadXml("<book genre = 'novel' ISBN = '1-861001-57-3'>" + "<title> Pride And Prejudice</title>" + "</book>");
//创建注释
XmlComment newComment
newComment = doc.CreatComment("sample XML document");
//把这个注释加进去
XmlElement root = doc.DocumentElement;
doc.InsertBefore(newComment, root);
5 创建并添加元素
XmlDoucment doc = new xmlDocument();
doc.LoadXml("<book genre = 'novel' ISBN = '1-861001-57-3'>" + "<title> Pride And Prejudice</title>" + "</book>");
XmlElement elem = doc.CreatElement("price");
XmlText text = doc.CreateTextNode("19.32"); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
doc.DocumentElement.AppendChild(elem); //将该节点添加到给定节点(此处是book 节点)的子节点列表的末尾
doc.DoucmentElement.lastChild.AppendChild(text);.
6使用SetAttribute
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book xmllns:bk='urn:samples' bk"ISBN= '1-33'>" +"<title>Pride and Prejudice</title>"+"</book>");
XmlElement root = doc.DocumentElement;
root.SetAttribute("genre","urn:samples","novel");
7使用CreatAttribute
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book xmllns:bk='urn:samples' bk"ISBN= '1-33'>" +"<title>Pride and Prejudice</title>"+"</book>");
XmlAttribute attr = doc.CreatAttribute("publisher");
attr.Value = "worldWilePulishing";
doc.DocumentElement.SetAttributeNode(attr);
例子6 和7 是创建新属性和添加的过程
方法6 获取元素节点并使用SetAttribute将属性添加到该元素的属性集合
方法7 使用CreatAttribute方法创建XmlAttribute节点,获取元素节点,然后使用SetAttributeNod方法添加
8 从DOM中移除节点
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book xmllns:bk='urn:samples' bk"ISBN= '1-33'>" +"<title>Pride and Prejudice</title>"+"</book>");
XmlNode root = doc.DocumentElement;
//把节点title移除
root.RemoveChild(root.FirstChild);
//移除所有的节点和属性
root.RemoveAll();
9 修改XML文档中的节点,内容和值
9.1 使用Value
XmlDoucment doc = new xmlDocument();
doc.LoadXml("<book genre = 'novel' ISBN = '1-861001-57-3'>" + "<title> Pride And Prejudice</title>" + "</book>");
XmlElement myElement = doc.DocumentElement;
//获取book 节点的所有属性
XmlAttributeCollection attrColl = myElement.Attributes;
//更改属性值利用Value
attrColl[0].Value= "poetry";
9.2使用ReplaceChild
XmlDoucment doc = new xmlDocument();
doc.LoadXml("<book genre = 'novel' ISBN = '1-861001-57-3'>" + "<title> Pride And Prejudice</title>" + "</book>");
XmlNode root = doc.DocumentElement;
XmlElement elem = doc.CreatElement("title");
elem.InnerText = "The Handmaid tale"; //注意和上面带@@@@@的区别和联系
//把title节点给替换了
root.ReplaceChild(elem,rooot.FirstChild);
10 保存文档
用OUtetXml属性
Xmldocument mydoc = new xmlDocument();
string xml= mydoc.OuterXml;
- XML操作集合
- XML操作集合
- C#操作XML方法集合
- C#操作XML方法集合
- C#操作XML方法集合
- C#操作XML方法集合
- StringBuilder,集合,泛型集合,目录操作,操作xml文件,委托,事件,程序集,反射
- C#操作xml的几种基本方法集合
- 集合操作
- 集合操作
- 集合操作
- 集合操作
- 集合操作
- 集合操作
- 集合操作
- 集合操作
- 集合操作
- 集合操作
- asp读取excel .xls格式文件
- vc下制作win32动态链接库
- 用asp.net c# 获取网页源代码
- ASP.NET中常用的26个优化性能方法
- 指定文件名读取WEB-INFO目录下properties配置(windows平台)
- XML操作集合
- 新蛋面试题
- ASP.NET缓存:方法分析和实践示例
- 11月28日某公司(移动平台游戏开发)笔试题
- #pragma 预处理指令详解
- 突破WINDOWS XP2 的IIS并发数
- inter portlet communication library
- js打开一个层
- “舞蹈家”怀特先生(Dance Dance Revolution)