寄托天下
查看: 12|回复: 0
打印 上一主题 下一主题

[未归类] 有没有谁比较了解XML中查找一个node的Xpath描述? [复制链接]

Rank: 9Rank: 9Rank: 9

声望
167
寄托币
8793
注册时间
2000-12-14
精华
21
帖子
88

Capricorn摩羯座 荣誉版主 挑战ETS奖章 QQ联合登录

跳转到指定楼层
楼主
发表于 2006-10-7 02:00:08 |只看该作者 |倒序浏览
需要查找的XML是这个

<?xml version="1.0" standalone="yes"?>
<XMLProducts>
  <products>
    <ProductID>1</ProductID>
    <ProductName>Chai</ProductName>
    <SupplierID>1</SupplierID>
    <CategoryID>1</CategoryID>
    <QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>
    <UnitPrice>18</UnitPrice>
    <UnitsInStock>39</UnitsInStock>
    <UnitsOnOrder>0</UnitsOnOrder>
    <ReorderLevel>10</ReorderLevel>
    <Discontinued>false</Discontinued>
  </products>
  <products>
    <ProductID>2</ProductID>
    <ProductName>Chang</ProductName>
    <SupplierID>1</SupplierID>
    <CategoryID>1</CategoryID>
    <QuantityPerUnit>24 - 12 oz bottles</QuantityPerUnit>
    <UnitPrice>19</UnitPrice>
    <UnitsInStock>17</UnitsInStock>
    <UnitsOnOrder>40</UnitsOnOrder>
    <ReorderLevel>25</ReorderLevel>
    <Discontinued>false</Discontinued>
  </products>
  <products>
    <ProductID>3</ProductID>
    <ProductName>Aniseed Syrup</ProductName>
    <SupplierID>1</SupplierID>
    <CategoryID>2</CategoryID>
    <QuantityPerUnit>12 - 550 ml bottles</QuantityPerUnit>
    <UnitPrice>10</UnitPrice>
    <UnitsInStock>13</UnitsInStock>
    <UnitsOnOrder>70</UnitsOnOrder>
    <ReorderLevel>25</ReorderLevel>
    <Discontinued>false</Discontinued>
  </products>
  <products>
    <ProductID>4</ProductID>
    <ProductName>Chef Anton's Cajun Seasoning</ProductName>
    <SupplierID>2</SupplierID>
    <CategoryID>2</CategoryID>
    <QuantityPerUnit>48 - 6 oz jars</QuantityPerUnit>
    <UnitPrice>22</UnitPrice>
    <UnitsInStock>53</UnitsInStock>
    <UnitsOnOrder>0</UnitsOnOrder>
    <ReorderLevel>0</ReorderLevel>
    <Discontinued>false</Discontinued>
  </products>
  <products>
    <ProductID>5</ProductID>
    <ProductName>Chef Anton's Gumbo Mix</ProductName>
    <SupplierID>2</SupplierID>
    <CategoryID>2</CategoryID>
    <QuantityPerUnit>36 boxes</QuantityPerUnit>
    <UnitPrice>21.35</UnitPrice>
    <UnitsInStock>0</UnitsInStock>
    <UnitsOnOrder>0</UnitsOnOrder>
    <ReorderLevel>0</ReorderLevel>
    <Discontinued>true</Discontinued>
  </products>
</XMLProducts>

然后在编程的时候,在一个listBox里列出来
                private void button1_Click(object sender, System.EventArgs e)
                {
               
                        //Create a new DataSet
                        DataSet ds = new DataSet("XMLProducts");
                        //read in the XML document to the DataSet
                        ds.ReadXml("..\\..\\..\\..\\prod.xml");
                        //load data into grid
                        dataGrid1.DataSource = ds;
                        dataGrid1.DataMember = "products";
                        //create the new XmlDataDocument
                        doc = new XmlDataDocument(ds);
                        //load the product names into the listbox
                        XmlNodeList nodeLst = doc.SelectNodes("//ProductName");

                        foreach(XmlNode nd in nodeLst)
                                listBox1.Items.Add(nd.InnerXml);
                }

然后鼠标在listBox1里面点击,选中哪个就取哪个的UnitPrice的值。
                private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
                {
                        //when you click on the listbox,
                        //a message box appears with the unit price
                        string srch = "XMLProducts/products[ProductName='"+
                                listBox1.SelectedItem.ToString()+"']";
                        XmlNode foundNode = doc.SelectSingleNode(srch);
                        if(foundNode!=null)
                                MessageBox.Show(foundNode.SelectSingleNode("UnitPrice").InnerText);
                        else
                                MessageBox.Show("Not found");
                }

但是我这个程序有一个毛病,就是如果    <ProductName>Chef Anton's Gumbo Mix</ProductName>
这种本身带 ' 符号的,"XMLProducts/products[ProductName='"+                                listBox1.SelectedItem.ToString()+"']" 就不灵了。(对于这个例子,就变成了"XMLProducts/products[ProductName='Chef Anton's Gumbo Mix']",这样SelectSingleNode就只会去搜索前面一段"XMLProducts/products[ProductName='Chef Anton')

这是《Professional C#》第二版 第11章 处理XML 的第四个例程 ADOSample4,原来的程序是让我使用这个Xpath语法
                        string srch="XMLProducts/Products[ProductName=" + '"' + listBox1.SelectedItem.ToString() + '"' + "]";
                        XmlNode foundNode = doc.SelectSingleNode(srch);
但是我试过了,这个也不对,对于<ProductName>Chef Anton's Gumbo Mix</ProductName>
srch的内容是"XMLProducts/products[ProductName=\"Chef Anton's Gumbo Mix\"]"
结果就是对所有的node都是弹出MessageBox "Not found"

不知道对于要寻找的node中已经包含'字符的情况Xpath有没有什么解决方法?

附上整个的程序
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.Data.SqlClient;

namespace Wrox.ProCSharp.DealWithXml.ADOSample4
{
        /// <summary>
        /// Summary description for Form1.
        /// </summary>
        public class Form1 : System.Windows.Forms.Form
        {
                private System.Windows.Forms.Button button1;
                private System.Windows.Forms.DataGrid dataGrid1;
                private System.Windows.Forms.ListBox listBox1;
                /// <summary>
                /// Required designer variable.
                /// </summary>
                private System.ComponentModel.Container components = null;
                private System.Data.SqlClient.SqlConnection sqlConnection1;
                private XmlDataDocument doc;

                public Form1()
                {
                        //
                        // Required for Windows Form Designer support
                        //
                        InitializeComponent();

                        //
                        // TODO: Add any constructor code after InitializeComponent call
                        //
                }

                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                protected override void Dispose( bool disposing )
                {
                        if( disposing )
                        {
                                if (components != null)
                                {
                                        components.Dispose();
                                }
                        }
                        base.Dispose( disposing );
                }

                #region Windows Form Designer generated code
                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                private void InitializeComponent()
                {
                        this.button1 = new System.Windows.Forms.Button();
                        this.dataGrid1 = new System.Windows.Forms.DataGrid();
                        this.listBox1 = new System.Windows.Forms.ListBox();
                        this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
                        ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
                        this.SuspendLayout();
                        //
                        // button1
                        //
                        this.button1.Location = new System.Drawing.Point(192, 328);
                        this.button1.Name = "button1";
                        this.button1.Size = new System.Drawing.Size(72, 24);
                        this.button1.TabIndex = 0;
                        this.button1.Text = "Load Data";
                        this.button1.Click += new System.EventHandler(this.button1_Click);
                        //
                        // dataGrid1
                        //
                        this.dataGrid1.DataMember = "";
                        this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
                        this.dataGrid1.Location = new System.Drawing.Point(16, 176);
                        this.dataGrid1.Name = "dataGrid1";
                        this.dataGrid1.Size = new System.Drawing.Size(256, 136);
                        this.dataGrid1.TabIndex = 1;
                        //
                        // listBox1
                        //
                        this.listBox1.ItemHeight = 12;
                        this.listBox1.Location = new System.Drawing.Point(24, 16);
                        this.listBox1.Name = "listBox1";
                        this.listBox1.Size = new System.Drawing.Size(248, 136);
                        this.listBox1.TabIndex = 2;
                        //
                        // sqlConnection1
                        //
                        this.sqlConnection1.ConnectionString = "workstation id=THINKPAD9528;packet size=4096;user id=sa;initial catalog=Northwind" +
                                ";persist security info=False";
                        //
                        // Form1
                        //
                        this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                        this.ClientSize = new System.Drawing.Size(288, 366);
                        this.Controls.Add(this.listBox1);
                        this.Controls.Add(this.dataGrid1);
                        this.Controls.Add(this.button1);
                        this.Name = "Form1";
                        this.Text = "Form1";
                        ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
                        this.ResumeLayout(false);

                }
                #endregion

                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                [STAThread]
                static void Main()
                {
                        Application.Run(new Form1());
                }

                private void button1_Click(object sender, System.EventArgs e)
                {
               
                        //Create a new DataSet
                        DataSet ds = new DataSet("XMLProducts");
                        //read in the XML document to the DataSet
                        ds.ReadXml("..\\..\\..\\..\\prod.xml");
                        //load data into grid
                        dataGrid1.DataSource = ds;
                        dataGrid1.DataMember = "products";
                        //create the new XmlDataDocument
                        doc = new XmlDataDocument(ds);
                        //load the product names into the listbox
                        XmlNodeList nodeLst = doc.SelectNodes("//ProductName");

                        foreach(XmlNode nd in nodeLst)
                                listBox1.Items.Add(nd.InnerXml);
                }
        }

                private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
                {
                        //when you click on the listbox,
                        //a message box appears with the unit price
                        string srch = "XMLProducts/products[ProductName='"+
                                listBox1.SelectedItem.ToString()+"']";
                        //string srch="XMLProducts/Products[ProductName=" + '"' + listBox1.SelectedItem.ToString() + '"' + "]";
                        XmlNode foundNode = doc.SelectSingleNode(srch);
                        if(foundNode!=null)
                                MessageBox.Show(foundNode.SelectSingleNode("UnitPrice").InnerText);
                        else
                                MessageBox.Show("Not found");
                }
}
有兔爰爰,稚离于罗。我生之初,尚无为。我生之后,逢此百罹。尚寐无吪!
有兔爰爰,稚离于罦。我生之初,尚无造。我生之后,逢此百忧。尚寐无觉!
有兔爰爰,稚离于罿。我生之初,尚无庸。我生之后,逢此百凶。尚寐无聪!

https://bbs.gter.net/thread-779599-1-1.html
英语学习一大误区
0 0

使用道具 举报

RE: 有没有谁比较了解XML中查找一个node的Xpath描述? [修改]

问答
Offer
投票
面经
最新
精华
转发
转发该帖子
有没有谁比较了解XML中查找一个node的Xpath描述?
https://bbs.gter.net/thread-536490-1-1.html
复制链接
发送
回顶部