Archive for the ‘xml’ Category

Flickr .Net API

26 November, 2007

 
Ran into a few permissions problems with this over the weekend when I came to deploying it with ASP.NET

FlickrNet. It is not incredibly web friendly – you need to override where it looks to store its cache. In your root web.config, you need to add the following section to the configSections part, or if there is no configSection piece, add it to the top of the web.config, right after the configuration section starts:

<configSections>
    <section name=”flickrNet” type=”FlickrNet.FlickrConfigurationManager,FlickrNet” />
</configSections>

And then add this line within the configuration section:

<flickrNet cacheLocation=”e:\domains\qgyen.net\temp” />

There are a few other issues. First, if you are running under medium trust, with the default configuration, you cannot define configuration sections. Second, if you are in medium trust, the cache location will need to be somewhere within your application’s directories. Medium trust often doesn’t let you read or write outside of the application. Third, you cannot use a string like “~/temp” you need to specify the full path.

jQuery Ajax call and result XML parsing

2 November, 2006

I have found jQuery to be one of the most powerful JavaScript libraries out there at the minute. I’ve been working with the base library and a number of plug-ins over the past couple of weeks and have found both the effects and AJAX methods to be both powerful and easy.

Here’s some code that I have used to look up an address. The data is served by an .aspx (.NET) page and I have included an example of the returned XML below.

jQuery code:

//Perform Ajax call – notice the number and postcode – these end up as querystring params and are taken in by AddressFinder.aspx
//’xml’ is an XML document object holding the returned XML
jQ.ajax({ type: “POST”, url: “AddressFinder.aspx”, data: “number=1&postcode=wn58ln”, dataType: “xml”, success: function(xml){

//This simple XPath XML function looks through the returned XML data
//All tags can now be accessed within the loop
var _number = “”;
var _line1 = “”;
var _town = “”;
var _city = “”;
var _country = “”;

//This function will loop for each match on addresses/address
jQ(“/addresses/address”, xml).each(function(){

_number = jQ(“number”, this).text();
_line1 = jQ(“line1″, this).text();
_town = jQ(“town”, this).text();
_city = jQ(“city”, this).text();
_country = jQ(“country”, this).text();

jQ(“#text11″).val(_line1);
jQ(“#text12″).val(_town);
jQ(“#text13″).val(_city);
jQ(“#text14″).val(_country);
});

XML returned from AddressFinder.aspx:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<addresses>
<address>
<number>6</number>
<line1>Almsberry Cresent</line1>
<town>Ashton – in – Makerfield</town>
<city>Wigan</city>
<country>England</country>
</address>
</addresses>

Did you find this demonstration helpful? If so leave me a comment and let me know if there are any other AJAX exmaples you’d like to see here. Thanks.

XML best practice reminders

18 September, 2006

Example Document

<?xml-stylesheet type="text/xsl" href="name.xsl"?>
<!-- Aaron Skonnard's name structure -->
<x:name xmlns:x="http://example.org/name">
<first>Aaron</first>
<!-- middle initial optional -->
<last>Skonnard</last>
</x:name>
<!-- end of name -->

Processing Non-Repeating Elements

One way is to call Read to move past the text node and then ReadEndElement to consume the element’s end tag marker. The pattern for dealing with text-only elements is shown here:

r.ReadStartElement("first");
Console.WriteLine("first: {0}", r.Value);
r.Read(); // moves past text node
r.ReadEndElement(); // first

This pattern can be summarized into the following steps:

  1. Call ReadStartElement to consume the start tag
  2. Retrieve the element’s text content through the Value property
  3. Call Read to move off the text node
  4. Call ReadEndElement to consume the end tag

To simplify using this pattern, the designers introduced another helper method called ReadElementString that encapsulates this behavior. Using ReadElementString makes it possible to simplify the code even further:

XmlTextReader r = new XmlTextReader("name.xml");
r.ReadStartElement("name", "http://example.org/name");
Console.WriteLine("first:{0}", r.ReadElementString("first"));
Console.WriteLine("last: {0}", r.ReadElementString("last"));
r.ReadEndElement(); // name

You’d be hard-pressed to simplify the code more than this.

Repeating Elements

Dealing with repeating elements also presents a problem since you have to check the name of the next element before committing to the ReadStartElement or ReadElementString call. The following code illustrates how to process the name element assuming it may contain zero or more first elements followed by a mandatory last element:

XmlTextReader r = new XmlTextReader(@"name.xml");
r.ReadStartElement("name", "http://example.org/name");
bool more=true;
while (more)
{
r.MoveToContent();
if (r.LocalName.Equals("first"))
Console.WriteLine("first: {0}",
r.ReadElementString("first"));
else
more=false;
}
Console.WriteLine("last: {0}", r.ReadElementString("last"));
r.ReadEndElement(); // name