数码志's profile爱页工作室PhotosBlogListsMore Tools Help
    August 28

    C#操作Xml的两种方式 XPath XmlDocument XmlNodeList

    <?xml version="1.0" encoding="utf-8" ?>
    <Company>
    <Department >
    <Name>IT Department</Name>
    <Manager>猪头三</Manager>
    <Employees>
    <Employee>
    <ID code="001" >10001</ID>
    <Name>西门庆</Name>
    <Gender></Gender>
    </Employee>
    <Employee>
    <ID code="002">10202</ID>
    <Name>潘金莲</Name>
    <Gender></Gender>
    </Employee>
    </Employees>
    </Department>
    </Company>

    需要取得 Name 为 “西门庆” 的 Employee 节点,用XPath实现如下:

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load( Path.Combine( Environment.CurrentDirectory, "demo.xml" ) );
    XmlNode emp = xmlDoc.SelectSingleNode( "/Company/Department/Employees/Employee[Name='西门庆']" );
    //emp 即为 定位到的 Employee 节点

    需要取得 code 为 002 的 Employee 节点, 用 XPath 实现如下:

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load( Path.Combine( Environment.CurrentDirectory, "demo.xml" ) );
    XmlNode emp = xmlDoc.SelectSingleNode( "/Company/Department/Employees/Employee/ID[@code='002']/parent::node()" );

    XPath 寻径简介

    xml文件,是一种树状结构, XPath 是针对xml文件寻径的一种 pattern。以开头的xml数据为例,下面给出几个常用的情形:

    • 取得所有的 Employee
    /Company/Department/Employees/Employee

    XPath的开头是一个斜线(/)代表绝对路径

    • 取得所有的 Name,不分层次
    //Name

    XPath 以 // 开头表示不限层次的一种模式

    • 使用 * 匹配未知名称的元素(不能匹配未知层级)

    1. 取得所有的 Employee

    /Company/Department/Employees/*

    2. 取得Department下,包含有 Employee 作为子节点的节点

    /Company/Department/*/Employee
    • 使用 [] 选择分支

    XPath中的元素索引,是从 1 开始的

    我们要选择 第一个 Employee

    /Company/Department/Employees/Employee[1]

    选择最后一个Employee

    /Company/Department/Employees/Employee[last()]

    选择叫西门庆的Employee

    /Company/Department/Employees/Employee[Name='西门庆']
    • 多路选择

    XPath 用 | 或者 Or 进行多路选择

    /Company/Deparment/Manager | /Company/Deparment/Name
    • 选择属性

    XPath中的属性,使用@开头

    选择所有的 code 属性

    //@code

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://ayeah.spaces.live.com/blog/cns!F87586FE3F932460!268.trak
    Weblogs that reference this entry
    • None