Parsing XML with C#

This has to be quick because I have things to do and this tangent, although a good teaching lesson to myself, has to wrap up so I can get back to the grind.

To me XML should be simple but gets all complicated with namespaces, XSD, XSLT, etc…  I’m kind of like a detective and want “Just the facts DATA ma’am”.  Secondly, I’ve never really seen the purpose of LINQ and usually just use datatables as they are simple and easy to use.  However, when you are given lemons XML, you make lemonade LINQ queries.  It will save you some frustration.

So if you have this XML…

<?xml version=”1.0″?>
<itemName>Item 1</itemName>
<itemName>Item 2</itemName>

You can select different elements using LINQ to XML.  Name you use XElement.  Do not confuse XElement with XmlDocument.  XmlDocument is older and will only serve to confuse you.

To select all the itemName’s and add them to a string you would…


After running, the variable R would be equal to…

This above Linq says [Select all elements from XE’s descendants that are named “itemName”].  It’s very reminiscent of a SQL statement.

Then we piece together a string called “R” that adds the value of each element to itself.

Now you know the basics.  You can iterate differently in your “foreach” statement.  You don’t have to use el.Value.  You could find different sub-elements if they existed, load it into a class or do whatever but there is one more thing that got me when I first learned.  That is “xmlns”.

So if this line…
…was replaced with this line
<items xmlns=”http://SomeWebsite/Version1.0″>
Then your code wouldn’t work.

The code wouldn’t work because even though <itemName> still looks like “<itemName” in the file, it is not.

Now this may seem confusing because if you open the XML file you will see <itemName> all over the place.  However, because you added an XML Namespace (ie. xmlns=”http://SomeWebsite/Version1.0”) LINQ now considers the name to be…

This may seem like it’s an over complication when you just want the data, however when there are different versions of elements this kind of versioning saves headaches down the road.

Hopefully this simplifies things for you.  It would have saved me some Googling if I would have found something similar earlier.

NCAA Basketball championship 2015 spreadsheet with odds

After finding some odds from KenPom here, I through them in a spreadsheet, added the Vegas odds as well as my own “Geiger Rankings™”

Geiger Rankings™ are other ranks weighted by the Vegas odds.  If you are in pools, this should really help give you some insight.

How to kill a frozen linked sql server connection

So I recently setup a Linked Server in our SQL database that connects to some sort of version of IBM’s DB2.

I made the mistake of querying a ginormous table.  After 30 minutes or so, I cancelled the query.  The query wouldn’t cancel so I closed the window in SQL Server Management Studio (SSMS).  I opened a new window and proceeded to query again with an added where clause.  I repeated this a few times with more and more restrictive where clauses.  After a while I closed SSMS and reconnected from a different machine.  I checked the activity monitor in SQL and saw that I had 5 connections.  I proceeded to “Kill” all of them.

Then this happened.

All the processes where stuck in KILLED/ROLLBACK and had a wait of PREEMPTIVE_OLEDBOPS.

At this point most SQL experts will blindly give you the advice that you should just “Wait” until the rollback is complete.  However, this was a linked server.  I wasn’t doing anything but select data from it.  I waited an hour or 2 an still nothing.  I checked the rollback status of each connection and each was at 0%.  Also, the CPU was pegging on the database server.  What was I to do?

Well the solution for me was to download TCP View from Microsoft.

I opened TCP View up and found the connections that weren’t working.  I killed the first one and the rest just disappeared.  My CPU went from almost all down to 2%.  (Note: It gave me a different machine name in TCP View because the machine in question had multiple DNS names that went to the same IP Address)

An enterprise level database should not allow an unreturned linked server to compromise the entire server.  If you agree, write to them here.

Windows guy learns linux How to telnet/ssh

So far I have used the VirtualBox console to connect to my virtual machine running Linux (CentOS 7).  This is fine and good for one session, but if you want to run multiple terminals you have to install a Telnet/SSH client.

I will be using a client called “Putty” which is free and very widely used.  Download Putty.exe to your desktop.

Now we have Putty now but it’s useless until we make some changes on our local virtual machine.  (If you’ve installed Linux on a real physical machine or had Linux installed on a server VM then this next steps might not apply)

We need to know which IP address our Linux server is running on.  In your VM console run “#ifconfig”.  This is very similar to running ipconfig on Windows command line.  My results are below.

Linux IFConfig

Note, the IP address of the virtual machine is  Now that we know that we go to VirtualBox, select the VM in question and click on Settings and then select “Network”.

Expand the “Advanced” tab if it is not already showing and click on “Port Forwarding”.

Virtual Box Linux SSH Port Setup

Here, click the +Plus sign and add a new rule.  It can be called whatever, I called mine SSH.  Add the line as shown.  If you have a different VM IP than then you will want to use that.

Virtual Box Linux SSH Port Forward Setup

At this point you can run that Putty.exe that you downloaded earlier.  Enter as your IP address and click “Open”.

Putty SSH Linux VirtualBox

At this point you should get a “login as:” prompt.  Enter your username and password and you should be connected.

Putty CentOS 7 VirtualBox

Now you can open up multiple terminal windows and run multiple programs at once.

If your terminal keeps timing out too early for you, you can change it so that it “stays alive” for a much longer time by doing something like this.

Open the sshd_config file.

# nano /etc/ssh/sshd_config

Add/change the following lines

ClientAliveInterval 30
TCPKeepAlive yes 
ClientAliveCountMax 99999

3/2/2015 Edit: Despite my changes, my terminal sessions were still getting disconnected.  For me the problem was with VirtualBox.  I had to download the beta version (4.3.23 r98674).  Then I could not launch any VMs because I would get a STATUS_OBJECT_NAME_NOT_FOUND error having to do with \Device\VBoxDrvStub.  I then uninstalled Windows Update KB3004394 and then reinstalled VirtualBox.  Now things seem better.  Hope that anybody having this problem finds this update helpful.

Windows guy learns linux centos network issues

So it was mandated by the “Linux Dudes” at work that they don’t really want Ubuntu installed in production.  RedHat is what they use.  The free version of RedHat is called CentOS.  CentOS is what I will be using going forward.  I don’t know enough to debate the pros and cons of one version of Linux over another but I am sure going to miss the nice graphic interface of Ubuntu.  For reflection, this is the CentOS interface.

CentOS Terminal

The first thing to note for CentOS other than the lack of graphical interface is that you manually have to setup a network adapter in the setup or it the OS won’t have any way of accessing the network/internet.

It was a super easy fix once I knew what I had left out.  Here’s some quick screenshots of how simple it is to install a network adapter in CentOS.

CentOS 7 Language Selection

CentOS 7 Add Network Adapter

CentOS 7 Enable Ethernet

CentOS 7 Network Connected

Windows guy learns linux hosts files

The hosts file is like a local DNS that takes priority over the real DNS.

The average person won’t understand what that means.  In other terms, the hosts file is a local map of “domain name” to an “IP address”.  For example, normally would resolve to something like  If you go to you would see Google. is the same as

Now if you went into your host file and put an entry like…
Then Google would point to Bing instead.

If you set to point to then in that case it would point to your local Apache server.

To create a map, we enter the URL and IP address in the hosts file that lives in the /etc directory.  (ie. /etc/hosts)

To modify the file we will use a text editor called nano.  It is much easier than some of the other options.   The hosts file is protected so we need to run as SUDO.  So get to a terminal (Ctrl+Alt+T in Ubuntu) and run the following command.

#sudo nano /etc/hosts

You then enter your root password and can then add the line. [TAB]

[TAB] is the tab key, not actually the word “[TAB]”


Hit Ctrl+O to save the file and then Ctrl+X to exit.

Now if you go to “” (not because we did not create an entry for that) you should see something like this…


Boom, all done.  Now you’re a hosts file expert.  Make sure to delete that line if you want to use Google again normally.

WINDOWS GUY take on LINUX webserver and mail server

Note: This article is using Ubuntu 14.04 but most will apply to other Linux versions.

So, we have learned how to install packages on Debian Linux and now want to configure mail and websites for this server.

Launch Terminal (Press Ctrl-Alt-T)

#sudo apt-get update
(This updates our local package list)

#sudo apt-get install lamp-server^
(This installs Apache, MySQL and PHP)
(Mind the caret (^) at the end.)

#sudo apt-get install mailutils
(This is for mailx)

#sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/default
(This copies the default Apache config file to a new file that will be used by Apache)

By running those 4 commands you have Apache, MySQL, PHP and Mailx installed.  To test apache you can open up your web browser and go to http://localhost to see this…
Ubuntu Apache Test

There are more things that go into setting up the mail server if you want to have it reside on the internet.  However if you are on a small work network, you can start sending test email immediately by running.

#mail –s “Some Subject”

Some message…

Like this…
Test Linux mailx

If you try this above example on a home network, or if you aren’t setup to relay mail on your work network, this test won’t work.  There’s a lot more to setting up email but this is the most basic of installs.

windows guy take on linux root user and packages

Note: This post mentions Ubuntu root access but most of it is about Linux in general.

Linux and Windows differ when it comes to login accounts that have full access to the system.  In Windows,  when you add a user to the local “Administrators” group, that user has complete access to all files and settings.  For good or bad.  Linux doesn’t really do it that way.

In Linux there is a “Root” user.  This root user has full access to everything.  However, every Linux admin will yell that you shouldn’t log into a computer as “Root”.   Instead you login as your self, then run a program called “sudo”.  Sudo runs the next command as root.   If I run..
#sudo ls
I will first get a prompt for “Root” password.  I type that, hit enter and then ls runs.


Tangent: In Linux you run “Terminal” in Windows you run “Command” or “Command Line”.  Terminal command start with a pound sign, ie “#”.  So if you see… #sudo blahblahblah it means run “sudo blahblahblah” from the terminal/command line.

The next thing to note is that there are “packages” for a type of Linux called “Debian”.  Everything that gets installed in a “Debian Linux” is a package.  Ubuntu is built on top of Debian so software in Ubuntu is installed from packages.

Q: How do you install packages on Debian Linux?
A: You use APT (Advanced Package Tool)

APT, in simplest terms, lets you install, remove and update packages.

#sudo apt-get update

The above command runs “apt-get update” with “root” rights.
“apt-get update” downloads a list of all packages and updates the local lists.  So when you want to know what new packages are available or if there are updates to your existing packages, just run sudo apt-get update.

More examples of how to install packages to follow.

windows guy initial take on ubuntu

Ubuntu was super easy to install.  It was all graphical.  It had very little options and networking worked right out of the box.  I was impressed that Firefox even worked because I installed Ubuntu in VirtualBox at work which is behind a proxy server.  The only problem was this…


No, I did not shrink the image.  That is the only screen resolution available to me.  I had to do some searching to find out how to change that.  Here are the 2 options of changing adding more resolutions to Ubuntu on VirtualBox. 

Note: To “Search” you simply press the “Windows” key on your keyboard then start typing.

Add More Resolutions to Ubuntu on VirtualBox

Search and run “xdiagnose”
- Extra Graphics Debug Messages
- Display boot messages
- Enable automatic crash bug reporting
- Restart

Search and run Software & Updates
Click “Additional Drivers” tab
Select “Using x86 virtualization solution…”
Apply Changes

I did “both” of the above options.  According the my searches either would have worked.  Regardless I now have more resolutions to play with.

It is worth noting that Linux guys and Windows guys have a point of contention with issues like this.  Windows guys want this “Basic” stuff just to work right out of the box.  Linux guys don’t seem to mind searching for the answers for a few minutes.  It’s interesting to note, that while this is a minor issue, it is very disheartening to Windows people.  This is probably because there’s so much “good stuff” to learn in Linux that they give up when walls are thrown up at them even before they get into the real guts.

Geiger, you don't know me! (Yet)

Web Analytics