XPath是XML的查询语言,其内容相当复杂。可以查阅www.w3.org/TR/xpath。
下面以一个实例简单了解一线XPath的查询方法:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | publicpartialclassForm1 : Form{    privateXmlDocument document;    publicForm1()    {        InitializeComponent();        document = newXmlDocument();        FileStream fs = newFileStream("xmlFileSelect.xml", FileMode.Open, FileAccess.Read);        document.Load(fs);        this.Update(document.DocumentElement.SelectNodes("."));    }    privatevoidUpdate(XmlNodeList nodes)    {        if(nodes == null|| nodes.Count == 0)        {            textBox1.Text = "no result";            return;        }        stringtext = string.Empty;        foreach(XmlNode node innodes)        {            textBox1.Text= Format(node, text, "");        }    }    privatestringFormat(XmlNode node, stringtext, stringindent)    {        if(node isXmlText)        {            text += node.Value;            returntext;        }        if(string.IsNullOrEmpty(indent))        {            indent = "";        }        else        {            text += "\r\n"+ indent;        }        if(node isXmlComment)        {            text += node.OuterXml;            returntext;        }        text += "<"+ node.Name;        if(node.Attributes.Count > 0)        {            AddAttribute(node, reftext);        }        if(node.HasChildNodes)        {            text += ">";            foreach(XmlNode child innode.ChildNodes)            {                Format(child, text, indent + " ");            }            if(node.ChildNodes.Count == 1 && (node.FirstChild isXmlText || node.FirstChild isXmlComment))            {                text += "\r\n"+ indent + "</"+ node.Name + ">";            }        }        else        {            text += "/>";        }        returntext;    }    privatevoidAddAttribute(XmlNode node, refstringtext)    {        foreach(XmlAttribute attribute innode.Attributes)        {            text += " "+ attribute.Name + "=‘"+ attribute.Value + "‘";        }    }    privatevoidbutton1_Click(objectsender, EventArgs e)    {        try        {            XmlNodeList nodes = document.DocumentElement.SelectNodes(textBox1.Text);            Update(nodes);        }        catch(Exception error)        {            textBox1.Text = error.ToString();        }            }} | 
主要实现根据输入内容检索xmlnode
原文:http://www.cnblogs.com/chenshizhutou/p/6715360.html