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