惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - 董昊(昊子)

好网站 软件项目典型风险一览 我的2008 flex下的树形下拉框treecombobox - 董昊(昊子) - 博客园 .NET牛人应该知道些什么?答案二 .NET牛人应该知道些什么?答案一 对大专起点学生的建议 7月6号宁波.net俱乐部聚会 本周日7月6号,宁波俱乐部聚会啦 [转]Web Farm中异步、高效的用户登录解决方案 [转]NET Framework:Application Domain 感谢华育国际,7月6号9点宁波.net俱乐部聚会 [转].NET牛人应该知道些什么 === 三个等号意味着严格等于 男儿当如土耳其 关于Web GUI界面...... 祝贺宁波.net俱乐部聚会圆满成功! Visual Studio 2008智能感知ExtJs ext-2.0扩展多选下拉框(代码及演示)
读取XML-致冷冽同学
董昊(昊子) · 2008-06-10 · via 博客园 - 董昊(昊子)

简易版

有xml

<sys_menu>

  <queryForm ID="queryForm" Text="查询页">
      <menu ID="soo" Text="搜索"  Cls="botton_search"  Width="50" Height=""  Img="Search.gif"  >
        <![CDATA[
        soo();
        ]]>
      </menu>
      <menu ID="new" Text="新建"  Cls=""  Width="" Height=""  Img="create.gif"  >
        <![CDATA[
        var text    = table_text+"编辑";
        var url     = "../edit_engine/edit_form.aspx?table="+zbm+"&id=-1&action=add";
        parent.addTab(zbm,text,url); 
        ]]>           
      </menu>
      <menu ID="close" Text="关闭"  Cls=""  Width="" Height=""  Img="efax_back.gif"  >
        <![CDATA[
        parent.close();
        ]]>           
      </menu>
</queryForm>

</sys_menu>

//c#代码

//处理节点
//_______________________Menu_________________________________
XmlNodeList nodeList= xmlDoc.SelectNodes("sys_menu/queryForm");


        //____________________________________________
        foreach (XmlNode node in nodeList)
        {
            if (node.Attributes != null)
            {
                ///////////////////////////////////////////////

                //获得各个节点的代码
                for (int i = 0; i < node.Attributes.Count; i++)
                {
                    string __name = node.Attributes[i].Name;
                    string __value = node.Attributes[i].Value;
                }
                string code=node.InnerText;//获得CDATA部分的代码

                //也可以这样获得获得各个节点的代码

                string ID = node.Attributes["ID"].ToString();;
                string Text = node.Attributes["Text"].ToString();;
                string Img = node.Attributes["Img"].ToString();;               
                ///////////////////////////////////////////////
            }
        }
        //__________________________________________________


原理版

XPath 是XML的查询语言,和SQL的角色很类似。以下面XML为例,介绍XPath 的语法。

 
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd country="USA">
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <price>10.90</price>
  </cd>
  <cd country="UK">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>9.90</price>
  </cd>
  <cd country="USA">
    <title>Greatest Hits</title> 
    <artist>Dolly Parton</artist> 
    <price>9.90</price> 
  </cd>
</catalog>
 
         

定位节点
XML是树状结构,类似档案系统内数据夹的结构,XPath也类似档案系统的路径命名方式。不过XPath 是一种模式(Pattern),可以选出 XML档案中,路径符合某个模式的所有节点出来。例如要选catalog底下的cd中所有price元素可以用:

 
/catalog/cd/price     
 

如果XPath的开头是一个斜线(/)代表这是绝对路径。如果开头是两个斜线(//)表示文件中所有符合模式的元素都会被选出来,即使是处于树中不同的层级也会被选出来。以下的语法会选出文件中所有叫做cd的元素(在树中的任何层级都会被选出来):

 
//cd
 

选择未知的元素
使用星号(Wildcards,*)可以选择未知的元素。下面这个语法会选出/catalog/cd 的所有子元素:

 
/catalog/cd/*
 

以下的语法会选出所有catalog的子元素中,包含有price作为子元素的元素。

 
/catalog/*/price
 

以下的语法会选出有两层父节点,叫做price的所有元素。

 
/*/*/price
 

以下的语法会选择出文件中的所有元素。

 
//*
 

要注意的是,想要存取不分层级的元素,XPath语法必须以两个斜线开头(//),想要存取未知元素才用星号(*),星号只能代表未知名称的元素,不能代表未知层级的元素。

选择分支
使用中括号可以选择分支。以下的语法从catalog的子元素中取出第一个叫做cd的元素。XPath的定义中没有第0元素这种东西。

 
/catalog/cd[1]
 

以下语法选择catalog中的最后一个cd元素:(XPathj并没有定义 first() 这种函式喔,用上例的 [1]就可以取出第一个元素。

 
/catalog/cd[last()]
 

以下语法选出含有price子元素的所有/catalog/cd元素。

 
/catalog/cd[price]
 

以下语法选出price元素的值等于10.90的所有/catalog/cd元素

 
/catalog/cd[price=10.90]
 

以下语法选出price元素的值等于10.90的所有/catalog/cd元素 的price元素

 
/catalog/cd[price=10.90]/price
 

选择一个以上的路径
使用Or操作数(|)就可以选择一个以上的路径。例如:

 
/catalog/cd/title | catalog/cd/artist
 

选择所有title以及artist元素

 
//title | //artist
 

选择所有title以及artist以及price元素

 
//title | //artist | //price
 

选择属性
在XPath中,除了选择元素以外,也可以选择属性。属性都是以@开头。例如选择文件中所有叫做country的属性:

 
//@country
         

选择所有含有country这个属性的cd元素:

 
//cd[@country]
         

以下语法选择出含有属性的所有cd元素

 
//cd[@*]
         

以下语法选择出country属性值为UK的cd元素

 
//cd[@country='UK']

只要掌握了xpath语法,理论上你就可以访问xml文件中的任意节点和任意值