| Subcribe via RSS

Error Message: Could not find file ‘c:\windows\system32\inetsrv\System.Xml.XmlDocument’.

January 13th, 2010 | Comments Off | Posted in XML

Someday I’ll free myself from the XML hell I seem to be mired in, but that day is not today. So I’ve got a fun new issue. The previous coder stored all the user entry values in session values without thinking about the fact that this won’t run correctly once it goes live in the web farm. So now I have to switch all those values over to be stored in the cookie and a few in the database that need to be secured. So in my haste to switch everything over, I ran across the error message “Could not find file ‘c:\windows\system32\inetsrv\System.Xml.XmlDocument’.” Huh? All I’ve done is try to store the Xml string into a XmlDocument object like so:

After pondering and tinkering with this for the better part of 30 minutes, it finally occured to me. The Load method of XmlDocument is used to access an Xml file on the server whereas the LoadXml method is used to load a previously defined Xml string. Popped that in, and it worked like a charm. Now what is my next error?

Delete Parent XML Node from Querying Child Attribute Using XPath & ASP.NET

July 9th, 2009 | Comments Off | Posted in ASP.NET, XML

So I’m receiving this XML string from an affiliate provider and evidently they don’t do any scrubbing on this data before kicking it my way. There are numerous instances of garbage node (i.e. test transaction – don’t put online). So I was tasked with cleaning up what they did not. I was looking to dig into the XML document and identify which child node innertext had the offensive text by querying the attribute hinged to that particular node. Take a look below at the XML structure I’m dealing with.

So in the following, say we wanted to delete all the Sleep Inn’s. We got a lumpy bed last time and the receptionist was smoking weed at the counter. Out you go Sleep Inn. The following code block accomplishes this.

So we’re using XPath to dig down to that description node. Once we are there, we create an XmlNodeList of all the nodes that fit this condition set in the XPath statement. Then we find which of these description nodes have “Sleep Inn” within their InnerText so we can define a the parentNode and remove all those child nodes. In all it scalps everything down to the <hot> tag. If we also wanted to get rid of this empty stub, we’d do a Regex statement to replace the start and end tag with blank space. Not the most elegant solution, but it works.

Working with XSLTransform and ASP:XML Controls in ASP.NET

July 7th, 2009 | Comments Off | Posted in ASP.NET, XML, XSL

Its a bit criminal how long its been since I worked with XSLT. I had this hellish project early in my programming career building a XML/XSLT program in ASP (yeah it was a while ago). I think it scarred me so bad I never had a desire to touch the stuff again. Really since you are able to read XML data straight into GridViews, there isn’t a whole lot of instances in my day to day coding life where I can’t just get by with that. Well today I had an issue where I was getting an XML datafeed from an affiliate provider and trying to find the best way to consume it. The big gotcha that kept me from going the easy GridView route was the format the data was in. It is ugly. The defining values are housed in the attributes and the node names are all the same (see below). What numbnuts designed this thing?

Anyway after trying to traverse the nodes, regenerate the XML document and other generally painful things to get the data to cooperate, I had a eureka moment to just plug it into an XSLT stylesheet. Talk about making my life incredibly easy. Here is the grand total of what it took to get the two elements plugged into my ASPX page. Notice the top 4 lines are my call to the affiliate site to get the XML dataset and line 5 is plugging the XML into the asp:XML control. Basically, one line of code. I like it.

Next, its time to dig into the XSLT. I've stripped this down to the essentials so we can focus on what is going on. Our xsl:for-each statement on line 15 drops down into the /response/result/hot node then our xsl:when tests to insure we bypass any test records the affiliate provider may have included. Next, we loop through the str node and pull out all of our relevant attributes (i.e. str[@name='description']). On line 41, we throw in our catchall for those datasets that come back without records.

All in all its pretty straight forward. I had lots of bells and whistles I had to incorporate to make this makeshift GridView work like it traditionally would under ASP.NET. Over the next few days, I'll post these extras.

Tags: , , , ,

Inserting XML into ASP.NET DataList

March 3rd, 2009 | Comments Off | Posted in ASP.NET, C#, XML
No heavy lifting today just back to basics working with ASP.NET’s DataList control. So I’ve worked to convert the album listings of a popular music download site into an XML format so I can display them on my sites. Going the route of a DataGrid or Gridview would be too constraining for my needs since I want to display pictures and have two columns per row. I want to customize the layout in other words and that isn’t DataGrid’s strong suit. Enter the DataList. With the DataList, I can customize the data flow in the Item Template as well as set how many columns to repeat and in which direction. All this in very little code. First we need to declare the System.Data object so we can put our XML document into a DataSet. Once there we’ll bind it to the DataList which is defined here as dlAlbums. Now looking at our DataList, we set the properties for RepeatDirection as well as RepeatColumns to define how our cells are going to write to the table. From there, we pull in our values from the XML document, filling in our appropriate placeholders. It doesn’t get much easier than that. Tags: , ,