Category Archives: Uncategorized

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″?>
<items>
<item>
<itemName>Item 1</itemName>
</item>
<item>
<itemName>Item 2</itemName>
</item>
</items>

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…

image

After running, the variable R would be equal to…
C# LINQ XML RESULTS

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…
<item>
…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…
LINQ XMLNS Fixed

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.

https://docs.google.com/spreadsheets/d/1FW-SAJugyNv5EjghK-dKHWVejxQxSuf_mu1fWTwWGkg/pubhtml?gid=301639732&single=true

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.
image

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.
http://www.google.com/search?q=tcp+view&btnI

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.

https://connect.microsoft.com/SQLServer/feedback/details/187192/

Stolen Chase Card Drama

Stolen Chase Credit Card

Chase really needs to stop the advertising push and work on security and customer service.  Let me explain…

Last Wednesday I receive a 7:58 am text message from Chase asking me if I charge $1.00 to a “Specialty Retailer”.  I respond to it stating “No”.  They respond.  “We will call you ASAP”.  Chase’s “ASAP” is 1,445 minutes later (Over 24 hours).  I can’t pick up at the time but they are nice enough to automatically call  me back 55 minutes after that.  After me asking several times “If anybody is there” an automated message comes on the line and asks me to hold for an available person.  5 minutes later, they disconnect me stating “Nobody is available to talk”.  Odd… you called me Chase!  Why would you call somebody when you can’t talk?

They have never since used this number but have called my work phone numerous times during the weekend when nobody is in the building much less in my cubicle.

So today I call Chase back, they want to know your credit card number, mother’s maiden name, last 4 digits of my social security number and my date of birth.  I gave them everything but my DOB.  This seems sketchy.  I start telling the above drama to a women, Lindsey I think was the name, who is taken aback that I am not playing according to the script she is reading.

At this point, utter panic must have stricken Chase and some big executive hits the panic button disconnecting me.  Luckily another automated message comes on saying “If you were disconnected in error go F yourself call back.”  Oh thanks Chase.

Just called back.  Got a women named “Sugar”.  This should be just great…

Stolen Chase Credit Card Number

Shoemoney throwback

Just a fun note… over 8 years ago ShoeMoney had a contest to see if anybody could outrank a site for “ShoeMoney”.  I totally forgot that I came in 5th for MSN.  The blog page is still there http://shoemoney-contest.blogspot.com/

It wasn’t a big thing.  I won $65.  The methods I used probably don’t work that well any more but it was fun.

You can follow the contest here…

http://www.shoemoney.com/2006/05/21/can-you-outrank-me-for-shoemoney-i-got-1000-that-says-you-cant

http://www.shoemoney.com/2006/06/01/shoemoney-contest-really-getting-interesting

http://www.shoemoney.com/2006/06/01/ok-i-was-trying-to-play-it-off

http://www.shoemoney.com/2006/06/02/mini-seo-contests-end-today

http://www.shoemoney.com/2006/06/02/shoemoney-winners-are

Remove URL from wordpress excerpt

If you have URLs showing up in your excerpt simply add the following somewhere in the top of your theme’s index.php file.

<?php
function RemoveURL($string)
{
  return preg_replace(‘/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i’, ”, $string);
}
add_filter(‘the_excerpt’, ‘RemoveURL’);
?>

This will remove 99% of the URLs in your excerpts.

SQL Server extremely slow to restore

I recently had an issue where restoring a SQL Server database would take forever just to load the restore window.  Right clicking a database, going to Tasks –> Restore –> Database would take MINUTES just to load on a good day.

While I have upgraded and patched the server it has been running since 2008. (You have to love VM Ware allowing you to update hardware and not reinstall).

Cause:
SQL Server logs all backups/restores to MSDB.  You can query all the information from the following system views.

msdb.dbo.backupfilegroup
msdb.dbo.backupmediafamily
msdb.dbo.backupmediaset
msdb.dbo.backupset

In my case because we have been running so many databases for so long I had over 1,000,000 records in msdb.dbo.backupset.  Any time that I used SQL Management Studio to restore a database, or if you deleted a database and instructed it to delete the backup history too, it would take FOREVER.

Resolution:
I created a simple job that deletes backups history over 90 days.  Just change “90” to however many days you want to delete and BLAMO everything is good again.

DECLARE @CutOff    DATETIME
SELECT @CutOff = DATEADD(day, -90, GETDATE())
EXEC SP_DELETE_BACKUPHISTORY @CutOff

Luxor Hotel Deals

Luxor Las Vegas
The Luxor in Las Vegas is a great mid-tier hotel in one of the most perfect locations.

…or you can click here to skip the reasons and go straight to the 30% off Luxor link.
(exp. 6/8/14)

Comfort
There’s a great pool, spa, salon etc…  Have poolside drinks next too a humongous pyramid or get a manicure and massage at a top rated salon.  There are enough ways to be pampered inside so you don’t have to wander out unless you choose to.

Gambling
I consider gambling at Luxor to be mid-tier.  It’s not too expensive and not too low brow.  However Luxor is in the perfect spot if you want to go high or limit.  To one side of Luxor is Excalibur which is on the low end with plenty of $5 tables and penny slots while still managing to be nice.  To the other side of Luxor is Mandalay Bay which is very high end but still extremely friendly.

Shopping
Between Luxor and Mandalay Bay is a great shopping center filled with specialty shops, restaurants and bars.  Perfect for splurging a little bit if you happen to win a little (or a lot) of money.  You never even have to go out in the blazing sun as this fun shopping corridor is all inside and climate controlled.

Entertainment
There are too many shows to list here in Luxor and the 2 connected hotels.  New York New York is very close as well and extremely walk-able in even the highest of high heels.  Everything else on the strip is a 5 minute cab ride.

Overall
You will love your stay!  One of the few places that you can overlook the entire casino from the hallway outside your room.  Great restaurants, shows, gambling and comforts await.

 Click here for a 30% off Luxor Coupon