Using XPath on a DataSet RRS feed

  • Question

  • NOTE: xdd and ds are class fields.


    I'm really new to XML manipulation and XML in general so bear with me.


    I made a simple GUI that allows a person to make an SQL query to a database and the results are filled in a DataSet.

    Then they can press a button to parse through the dataset using Xml techniques. Here is what I've got:


    private void btnCrtDS_Click(object sender, EventArgs e)


    string conStr = "Data Source=TWINKLE\\SQLEXPRESS;Initial Catalog=northwind;Integrated Security=True";

    SqlConnection conn = new SqlConnection(conStr);

    SqlCommand cmd = conn.CreateCommand();

    cmd.CommandType = CommandType.Text;

    cmd.CommandText = txtBxSQL.Text;

    SqlDataAdapter da = new SqlDataAdapter();

    da.SelectCommand = cmd;

    ds = new DataSet();

    da.Fill(ds, "Table1");

    dataGridView1.DataSource = ds;

    dataGridView1.DataMember = "Table1";

    xdd = new XmlDataDocument(ds);


    private void btnSelNodes_Click(object sender, EventArgs e)


    XPathNavigator xpn = xdd.CreateNavigator();

    XPathNodeIterator xpni = xpn.Select(txtBxXPath.Text);


    while (xpni.MoveNext())


    lbNodes.Items.Add(" " + xpni.Current.NodeType.ToString() +

    ": " + xpni.Current.Name + " = " +






    now for the text that I placed in the txtBxXPath, "//customerID"


    Should bring up a list of all the customerIDs in the dataset right?


    any help would be great. Smile

    Monday, April 28, 2008 10:29 PM

All replies

  • Why don't you just simply use the Select method for the DataTable since you have a DataSet?




    Monday, April 28, 2008 10:39 PM
  • Although you raise a entirely valid point, one which I agree with, it does not suit the purpose of my little exercise to do so. I'm trying to manipulate XML objects in a multitude of different ways and this happens to be one of them. I want to get the CustomerIDs using the XPath "query langauge".


    This is all for exposure and learning purposes so please bear in mind that although the example may make next to little or no sense .. it allows me to learn how to use the feature set for XML in C#



    Tuesday, April 29, 2008 1:00 AM
  • You would have to display the xml... Probably you need to a add a namespace (use the NameSpaceManager) so that customerID is known... (Doesn't the DataAdapter add a namespace http://tempuri or something like that?)

    Tuesday, April 29, 2008 11:58 AM