C#高级编程:使用XmlDocument对象[1]
来源:互联网 发布:天龙八部挂机软件 编辑:程序博客网 时间:2024/05/01 08:48
XmlDocument及其派生类XmlDataDocument(详见本章后面的内容)是用于在.NET中表示DOM的类。与XmlReader 和 XmlWriter不同,XmlDocument具有读写功能,并可以随机访问DOM树。XmlDocument类似于MSXML中的DOM执行方式。如果您用MSXML编过程序,就会觉得使用XmlDocument很合适。注意,我们在本节的示例中添加了模块级的声明:
private XmlDocument doc=new XmlDocument(); 在这个示例中,与上一个示例一样,从books.xml 文档中加载了带有标题的列表框。单击列表框,会引发SelectedIndexChanged()事件,获取列表框中所选项的文本(书名),创建一个XPath 语句,把它传送给doc对象的SelectSingleNode() 方法,该方法返回title是书名的一部分的book元素(foundNode)。在消息框中显示节点的InnerText。继续单击列表框中的项目 ,此时文档已经加载,且一直到释放它之前,它都处于已加载状态。
下面介绍的示例创建一个XmlDocument对象,加载磁盘上的一个文档,再从标题元素中加载带有数据的列表框,这类似于XmlReader一节的示例,区别是本例选择要使用的节点,而不是像XmlReader示例那样浏览整个文档。
下面是该示例的代码,与XmlReader示例相比,这个示例是比较简单的(该文件在下载的DOMSample1文件夹中):
private void button1_Click(object sender, System.EventArgs e)
...{
// doc is declared at the module level
// change path to match your path structure
doc.Load("../../../books.xml");
// get only the nodes that we want
XmlNodeList nodeLst=doc.GetElementsByTagName("title");
// iterate through the XmlNodeList
foreach(XmlNode node in nodeLst) listBox1.Items.Add(node.InnerText);
}
...{
// doc is declared at the module level
// change path to match your path structure
doc.Load("../../../books.xml");
// get only the nodes that we want
XmlNodeList nodeLst=doc.GetElementsByTagName("title");
// iterate through the XmlNodeList
foreach(XmlNode node in nodeLst) listBox1.Items.Add(node.InnerText);
}
private XmlDocument doc=new XmlDocument();
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
...{
//create XPath search string
string srch="bookstore/book[title='" + listBox1.SelectedItem.ToString()
+ "']";
//look for the extra data
XmlNode foundNode = doc.SelectSingleNode(srch);
if(foundNode != null)
MessageBox.Show(foundNode.InnerText);
else
MessageBox.Show("Not found");
}
...{
//create XPath search string
string srch="bookstore/book[title='" + listBox1.SelectedItem.ToString()
+ "']";
//look for the extra data
XmlNode foundNode = doc.SelectSingleNode(srch);
if(foundNode != null)
MessageBox.Show(foundNode.InnerText);
else
MessageBox.Show("Not found");
}
下面简要介绍一下SelectSingleNode()方法,它是XmlDocument类的Xpath实现,SelectSingleNode ()和 SelectNodes()都是在XmlNode中定义的,而XmlDocument是基于XmlNode的。SelectSingleNode()返回一个XmlNode,SelectNodes()返回一个XmlNodeList。System.Xml.XPath命名空间包含许多Xpath实现。
msdn xpath参考:
- C#高级编程:使用XmlDocument对象[1]
- 使用GridView绑定XmlDocument对象
- 使用GridView绑定XmlDocument对象
- C#下使用XmlDocument详解
- C#高级编程:对象和类型
- C# 高级编程 对象和类型
- C# XMLDocument
- ASP.NET XML高级编程(二、XmlDocument)
- C# 高级编程读书笔记1
- c#高级编程读书笔记1
- C#温故而知新学习系列之XML编程—1-XmlDocument类
- XmlDocument使用
- C#中用XmlDocument对象获取XML文件中的节点值
- C#之XML基本读取(XmlDocument对象)
- C#轻松入门(四)面向对象的高级编程
- C#高级编程--比较对象的相等性
- C#高级编程读书笔记 - 第三章对象和类型
- C#高级编程学习笔记(二)---对象和类型
- 高校毕业生"三支一扶"计划 启动箱
- oracle的一些常见问题及处理
- 相关文章:
- Unix平稳 Windows欲甩Linux
- 4.2.6 文件路径验证
- C#高级编程:使用XmlDocument对象[1]
- 解密百度IM项目组“特种部队”
- JSON学习
- 4.2.5 排除两端存在空白字符的文件全名验证 (2)
- Eclipse报告内存不足和PermSize space错误,Eclipse死掉的解决办法
- php中经常用到的几个取整函数
- 如何预防arp欺骗以及c#如何自动绑定网关的mac地址?
- Google与IBM在云端相遇
- ADOQuery 导出到 EXCEL