Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Rob Lang http://www.maison-de-stuff.net/rob/

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Using cycling through input variables in PHP using foreach

This is a simple mechanism for cycling through all the inputs to a PHP script. If you're using $_GET (variables in the query string), $_POST (variables posted with a POST method on a form) or even $_SESSION (variables stored in the session) you can use this technique.

This snippet prints out all the variables in the $_GET variable (the query string) .

foreach ($_GET as $key => $value)
{
print "$key has a value of $value";
}

Using Rows.Find in ADO.NET

When using a DataTable in ADO.NET, you may want to search your table given a key. The Find method requires the index of the primary key to find the row that you're looking for. However, before you do this you must set the primary key of the Datatable. Setting the primary key in your database is not enough. To set the primary key, create a new column and then set the primary key field on the Datatable like so:


// Create the DataColumn list for holding the columns of the primary keys. In this case, we are using only one primary key
DataColumn[] primaryKeyColumns = new DataColumn[1];

// Set the first primary key to the column 'id' in the datatable
primaryKeyColumns[0] = myDataTable.Columns["id"];

// Now apply the primary key to the datatable
myDataTable.PrimaryKey = primaryKeyColumns;

Moving nodes from one XmlDocument to another

In .NET has some powerful tools to deal with Xml. All Xml nodes need to be created in the context of a XmlDocument object. If you need to move the nodes from one Xml Document to another, it's not obvious that you can just copy the nodes from one XmlDocument to another, after all Xml is just text isn't it.

Here's how to move all the nodes from one Xml Document to another:

// Dont forget this one:
using System.Xml;

// First, get some documents together
XmlDocument DocumentSource = new XmlDocument();
XmlDocument DocumentDestination = new XmlDocument();

// Load the documents with some nodes
DocumentSource.LoadXml("");
DocumentDestination.LoadXml("");

// Now to do the conversion
XmlNode tempNode = DocumentDestination.ImportNode( DocumentSource.FirstChild )

// Now insert the fragment into the document
DocumentDestination.FirsChild.AppendChild(tempNode);

From DataSet to XmlDocument

Ever had to take a DataSet from the database and turn it into an Xml Document in .NET? The process is a little messy because there is no direct conversion method. This is because Xml is streamed from the DataSet but the XmlDocument needs a string to load from. Here's my solution:

 // This is the final document
XmlDocument Data = new XmlDocument();

// Create a string writer that will write the Xml to a string
StringWriter stringWriter = new StringWriter();

// The Xml Text writer acts as a bridge between the xml stream and the text stream
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);

// Now take the Dataset and extract the Xml from it, it will write to the string writer
content.WriteXml(xmlTextWriter, XmlWriteMode.IgnoreSchema);

// Write the Xml out to a string
string contentAsXmlString = stringWriter.ToString();

// load the string of Xml into the document
Data.LoadXml(contentAsXmlString);

From XmlDocument to XPath Document

It's not obvious how you might turn your standard XmlDocument into an XPathDocument as there is no direct method to do so. The best way is to make use of a reader for the conversion:

XPathDocument xpathDoc = new XPathDocument (new XmlNodeReader(doc));
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS