Monthly Archives: October 2004

Using XPath to narrow results from an XML file

// These two lines read config setting from the app.config file. filename = ConfigurationSettings.AppSettings["inputfile"]; filter = Configu rationSettings.AppSettings["filter1"]; XmlDataDocument myDocument = new XmlDataDocument(); myDocument.Load(filename); XmlNodeList myList = myDocument.SelectNodes("//item[@class='" + filter + "']/*"); for (int i=0; i < myList.Count; i++) { label1.Text += "/r/n" + myList[i].InnerText; }

The associated xml file looks like this…

<?xml version="1.0" encoding="utf-8" ?> <itemlist> <item class="vegetable"> <name>Carrot</name> <colour>Orange</colour> </item> <item class="animal"> <name>Elephant</name> <colour>Grey</colour> </item> <item class="mineral"> <name>Quartz</name> <colour>white</colour> </item> </itemlist>

**edit**

notes:

// will match anywhere in the document.
/ will match the next element
./ will match the previous element.

Create a DataSet from a Stored Procedure

SqlConnection myConn = new SqlConnection(ConnectionString);
SqlDataAdapter da = new SqlDataAdapter();
ds.SelectCommand = new SqlCommand(“GetByOffice”,myConn);
da.SelectCommand.CommandType=CommandType.StoredProcedure;
SqlParameter myParam = new SqlParameter(“@Office”,SqlDbType.VarChar,50);
myParam.Direction=ParameterDirection.Input;
myParam.Value = textBox1.Text;
da.SelectCommand.Parameters.Add(myParam);
DataSet ds = new DataSet();
da.Fill(ds,”results”);
dataGrid1.DataSource = ds.Tables[“Results”];
ds.WriteXml(“result.xml”,XmlWriteMode.WriteSchema);