Reading XML that has namespaces
In trying to convert this blog I had to knock up an application to read dasBlog dayentry files and write out a large RSS style wordpress import file.
Now I don’t do XML that often but when I do its been with simple XML so a quick “//Entries” XPath query and away you go. However the dasBlog XML is done properly with lots of namespaces.
So when I’ve been doing my simple XPath query it returns nothing. So for those having similar problems the solution is this. You need a namespace manager.
Here is a quick code snippet that explains how you would use it.
XmlNamespaceManager mgr=new XmlNamespaceManager(xmlDoc.NameTable);
mgr.AddNamespace(“def”,”urn:newtelligence-com:dasblog:runtime:data”);foreach(XmlNode innerNode in xmlDoc.SelectNodes(“//def:Entry”,mgr) )
{
//Do things with innerNode;
title=innerNode.SelectSingleNode(“def:Title”,mgr).InnerText;
//Do more things
}
Simple as that.
tags: .net, c#, xml+programming, xml, namspace+manager
Do you still have the code to migrate your XML files? I’m looking to move from dasBlog to wordpress (dasblog running like a dog) and would prefer not to reinvent the wheel
Cheers
Owen
I mailed Owen that I hadn’t kept this code, it was such a throwaway thing to me, most of the time I would run stuff like this in studio tweaing as I debug then just ditch it, didn’t even cross my mind that it was worthy enough for publishing.
Admittedly the code did not have access to the attachments (as you may have found out from earlier posts) so was really only half of a proper solution.
So sorry.