XMLNameTable
XmlNameTable 类使 XmlReader 类的实现能够在分析数据或对 XML 文档执行比较操作时使用指针比较,而不是字符串比较。在比较和使用元素和属性名时,使用此表将提高 XmlReader 派生类的性能。XmlNameTable 还可以减少在分析期间分配的内存量,因为每个元素或属性的名称只分配一次内存。如果 XML 文档中多次出现相同的名称,将使用存储名称的字符串对象的相同实例。
使用 XmlNameTable
XmlNameTable 是一个抽象基类,NameTable 是它的一个实现。NameTable 包含元素和属性名的原子化版本以及命名空间 URI 和前缀。如果应用程序要对元素或属性的名称进行许多比较,应用程序应使用 XmlReader 中的 NameTable。用户可以从 System.Xml.XmlReader.NameTable 属性获取读取器正在使用的 NameTable。
应用程序可以使用 Add 方法将名称添加到该表中。下面的示例显示比较是通过使用 Equals 方法或 == 运算符完成的,它确定在方法调用中提供的对象是不是与当前对象相同的实例。
object cust = reader.NameTable.Add("Customer");
while (reader.Read())
{
if (cust == reader.Name)
{
/// ...
}
}
<?xml version="1.0" encoding="utf-8" ?>
<config>
<book>book1</book>
<price>18.50</price>
</config>

private void button2_Click(object sender, EventArgs e) { // Add the element names to the NameTable. NameTable nt = new NameTable(); object book = nt.Add("book"); object title = nt.Add("price"); // Create a reader that uses the NameTable. XmlReaderSettings settings = new XmlReaderSettings(); settings.NameTable = nt; XmlReader reader = XmlReader.Create("books.xml", settings); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { // Cache the local name to prevent multiple calls to the LocalName property. object localname = reader.LocalName; // Do a comparison between the object references. This just compares pointers. if (book == localname) { // Add additional processing here. string n = reader.ReadInnerXml(); label1.Text = n; } // Do a comparison between the object references. This just compares pointers. if (title == localname) { // Add additional processing here. string n = reader.ReadInnerXml(); label2.Text = n; } } } // End While // Close the reader. reader.Close(); }
posted on 2019-06-11 20:10 qq1151219115 阅读(118) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗