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-4 of 4 total  RSS 

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:

   1  
   2  
   3  // Create the DataColumn list for holding the columns of the primary keys. In this case, we are using only one primary key
   4  DataColumn[] primaryKeyColumns = new DataColumn[1];
   5  
   6  // Set the first primary key to the column 'id' in the datatable
   7  primaryKeyColumns[0] = myDataTable.Columns["id"];
   8  
   9  // Now apply the primary key to the datatable
  10  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:

   1  
   2  // Dont forget this one:
   3  using System.Xml;
   4  
   5  // First, get some documents together
   6  XmlDocument DocumentSource = new XmlDocument();
   7  XmlDocument DocumentDestination = new XmlDocument();
   8  
   9  // Load the documents with some nodes
  10  DocumentSource.LoadXml("");
  11  DocumentDestination.LoadXml("");
  12  
  13  // Now to do the conversion
  14  XmlNode tempNode = DocumentDestination.ImportNode( DocumentSource.FirstChild )
  15  
  16  // Now insert the fragment into the document
  17  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:

   1  
   2   // This is the final document
   3  XmlDocument Data = new XmlDocument();
   4  
   5  // Create a string writer that will write the Xml to a string
   6  StringWriter stringWriter = new StringWriter();
   7  
   8  // The Xml Text writer acts as a bridge between the xml stream and the text stream
   9  XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
  10  
  11  // Now take the Dataset and extract the Xml from it, it will write to the string writer
  12  content.WriteXml(xmlTextWriter, XmlWriteMode.IgnoreSchema);
  13  
  14  // Write the Xml out to a string
  15  string contentAsXmlString = stringWriter.ToString();
  16  
  17  // load the string of Xml into the document
  18  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:

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