Wednesday, February 3, 2010

Converting SQL into an XML Document using C#

After seeing poor implementation of this I decided it would be a good idea to make a simple guide available.

First we will start by declaring the xmlDocument and what will be its root element

XmlDocument xmlDoc = new XmlDocument();
XmlElement root = xmlDoc.CreateElement("root");


Next we will jump over to the SQL command that will be used to get your data. Obviously this query below is just an example, make sure yours is relevant.

string sqlQuery = "SELECT col1, col2, col3 From myTable Where col3 <> ''";


Declare your SqlConnection. I suggest using your web.config to set this up. Technically you can do this multiple ways, but here are a couple.

1.
In web.config:

<appSettings>
<add key="sqlConnection" value="Data Source=192.168.0.1;Initial Catalog=myDB;User ID=myUser;Password=myPassword;"/>
</appSettings>

Then use the key to build the connection.

SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["sqlConnection"]);


Or option 2, which is considered to be more proper:
In web.config:

<connectionStrings>
<add name="sqlConnection" connectionString="server=192.168.0.1;uid=myUser;pwd=myPassword;database=myDB" providerName="System.Data.SqlClient" />
</connectionStrings>

Then using the connection string to build the connection:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConnection"].ConnectionString);


Honestly, I'm currently using option 1 for the application that brought this to mind. Mostly because I'm keeping my new connections in the same format used by the prior developer, while I work on more important changes.

After you declare your connection, start up a try/catch block, since we are going to open up the connection. Always a good thing to be safe around connections.

try{
conn.Open();


Once these are declared we are going to
Now it is time to start reading the sql data and giving the value to the nodes and attribute


SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand(sqlQuery, conn);
myReader = myCommand.ExecuteReader();
while(myReader.Read())
{


Now we want to declare the elements to be used in the xml. For the example I am going to do 1 row node with a child col node that has an attribute.


XmlNode row= xmlDoc.CreateElement("row");
XmlNode col1 = xmlDoc.CreateElement("col");
XmlAttribute col2 = xmlDoc.CreateElement("attribute");


After the nodes are declared we can assign the values


//attribute value assignment
col2.value = myReader["col2"].ToString();
//node value assignment
col1.InnerText = myReader["col1"].ToString();


Then once you have your nodes containing the proper values, you can append them

//Append the Attribute to col1
col1.Attributes.Append(col2);
//Append col1 to the row
row.AppendChild(col1);
//Append the row to the root
root.AppendChild(row);


Now we can close up the while loop and try/catch

}
}catch(Exception ex){
//do something if you want
Response.Write(ex.ToString());
}


Then we can append the root to the xml document

xmlDoc.AppendChild(root);


Now you sucessfully have an xml document with values from the sql table. At this point you can use the document or save it using xmlDoc.save(path);
Here is the resulting format of your xml:

<root>
<row>
<col attribute='Col2 data'>Col1 data</col>
</row>
</root>

Full Code:

using System;
using System.Xml;
using System.Data.SqlClient;

public class SqlToXml
{
public XmlDocument getSqlToXml{
XmlDocument xmlDoc = new XmlDocument();
XmlElement root = xmlDoc.CreateElement("root");

string sqlQuery = "SELECT col1, col2, col3 From myTable Where col3 <> ''";

SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["sqlConnection"]);

try{
conn.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand(sqlQuery, conn);
myReader = myCommand.ExecuteReader();
while(myReader.Read())
{
XmlNode row= xmlDoc.CreateElement("row");
XmlNode col1 = xmlDoc.CreateElement("col");
XmlAttribute col2 = xmlDoc.CreateElement("attribute");

col2.value = myReader["col2"].ToString();
col1.InnerText = myReader["col1"].ToString();

col1.Attributes.Append(col2);
row.AppendChild(col1);
root.AppendChild(row);
}
}catch(Exception ex){
//do something if you want
Response.Write(ex.ToString());
}

xmlDoc.AppendChild(root);

return xmlDoc;
}
}

Stumble Upon ToolbarDigg this Post This to FacebookShare on Facebook Tweet Me

Saturday, April 18, 2009

Side Note: Verizon Wireless's Beaten Horse

As I was sitting at home watching a little TV this afternoon I see this commercial come on. Once again another Verizon Wireless Dead Zone commercial. Verizon has been beating this horse since 2008 and in my mind it doesn't look like it will be attracting any new customers. In this day and age the communications industry needs to be rapidly releasing new advancements in their service.




For cellular companies I think Sprint finally got the idea on their advertisements. Good job Sprint on realizing the future, rather than playing reruns of your bragging rights.

Stumble Upon ToolbarDigg this Post This to FacebookShare on Facebook Tweet Me

Thursday, April 2, 2009

Wireless interference got you down?

With all of the wireless traffic in our neighboring airways these days it can be difficult to get the same quality of signal you were previously receiving. Most average users are unaware that their wireless devices utilize frequency channels, some of which can be adjusted to use a different channel. However, when multiple devices are using the same channel you may experience some wireless interference. For those of you who may be a little lost by this, think about those times when you have gotten a ghost image of a TV channel into another channel. This is similar to the interference going on with your wireless devices.

There are a few different software programs which you can utilize to help identify which channel you should change your device settings to (when applicable), but I am only going to plug one here today.




inSSIDer by MetaGeek


"NetStumbler used to be our tool of choice for profiling a wireless network, but NetStumbler has stumbled. Today we favor inSSIDer."
-InfoWorld

*NetStumbler is another good program to help out with this, but just ignore that because it is not being plugged :)


inSSIDer's graphical system makes it a great application for identifying wireless interference. The graphs allow you to understand not only how strong a wireless signal is and what channel it is on, but how much interference it causes across the neighboring channels due to frequency bleeding.



I urge you to download this application and try it out for yourself, the results you get after changing to a better channel situated for your area will give you the satisfaction you deserve.

Stumble Upon ToolbarDigg this Post This to FacebookShare on Facebook Tweet Me