Using XPath to Query XML Containing Namespace Prefixes in .NET

Yesterday I was working on a small .NET console app for the IIN Video Department to convert Adobe XMP files with closed captioning data into Subrip SRT files for processing by encoding.com. The XMP file is in XML format and contains the timestamp, duration, and speaker of each word in a video transcript. My program needed to parse this data and combine the words into 2 lines of text that will be rendered per screen via closed captioning. The start time of each screen of text would be equal to the start time of the first word, and the end time would be equal to the end time of the last word. Each line would only be able to contain a fixed number of characters to avoid line wrapping in the video. Also, every time a new speaker starts talking, a new line would be created, and prefixed with the speaker’s name and a colon “<Speaker>:” A fun little project which I’ll post on Github when it’s done. However, I did come across the following resource which is useful when using XPath to query an XML document that uses namespace prefixes. Namespace prefixes are shortcuts used to qualify element and attribute names in an XML file, usually to avoid element name conflicts. To work with them in .NET you need to use System.Xml.XmlNamespaceManager. Here’s a Microsoft support article that show’s how it’s done: http://support.microsoft.com/kb/318545

Advertisements

About Martin Rybak

I am a New York area software developer and MBA with 10+ years of server-side experience on the Microsoft stack. I've also been a native iOS developer since before the days of ARC. I architect and develop full-stack web applications, iOS apps, database systems, and backend services.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: