<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://v900u039rux.maximumasp.com/Web/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>hB0 : XML</title><link>http://v900u039rux.maximumasp.com/Web/blogs/hb0/archive/tags/XML/default.aspx</link><description>Tags: XML</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP2 (Build: 20611.960)</generator><item><title>Process XML Dot Net -&gt; Parse to Object</title><link>http://v900u039rux.maximumasp.com/Web/blogs/hb0/archive/2007/09/11/process-xml-dot-net-gt-parse-to-object.aspx</link><pubDate>Tue, 11 Sep 2007 04:19:00 GMT</pubDate><guid isPermaLink="false">3790bee0-d05b-4b84-a272-3ed522a0473a:65</guid><dc:creator>hB0</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://v900u039rux.maximumasp.com/Web/blogs/hb0/rsscomments.aspx?PostID=65</wfw:commentRss><comments>http://v900u039rux.maximumasp.com/Web/blogs/hb0/archive/2007/09/11/process-xml-dot-net-gt-parse-to-object.aspx#comments</comments><description>&lt;p&gt;Here we consider a small aspect found while parsing XML string.&lt;br /&gt;
We are using Dot Net (1.1) but we process xml in general (non-dotnet specific version) using the XPath expressions.&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;Situation is we have XML string format as:&lt;br /&gt;
&amp;lt;ns:XMLMessage xmlns:ns=&amp;quot;http://domain.com/codename/MessagesXMLSchema.xsd&amp;quot;&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;ns:EventType&amp;gt;task&amp;lt;/ns:EventType&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;ns:EventName&amp;gt;TASK_COMPLETE&amp;lt;/ns:EventName&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;ns:JobId&amp;gt;02000016000004630144000100000070&amp;lt;/ns:JobId&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;ns:TaskId&amp;gt;02000016000004630154000200000070&amp;lt;/ns:TaskId&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;ns:EventDateTime&amp;gt;2007-09-06T15:26:25+04:00&amp;lt;/ns:EventDateTime&amp;gt;&lt;br /&gt;
&amp;lt;/ns:XMLMessage&amp;gt;&lt;br /&gt;
&amp;nbsp;= strXMLMsg&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;Scene 1:&lt;br /&gt;Dim xmlDoc As New Xml.XmlDocument&lt;br /&gt;xmlDoc.LoadXml(strXMLMsg)&lt;br /&gt;strXPath = &amp;quot;/XMLMessage /EventName&amp;quot;&lt;br /&gt;xmlNode = xmlDoc.SelectSingleNode(strXPath)&lt;br /&gt;strData = xmlNode.InnerText&lt;br /&gt;In this seen we will not be able to get our parsed data. And the issue is the inclusion of the namespace and its prefix.&lt;br /&gt;
So to get the parsed data correctly we have to use the way shown in scene 2.&lt;/p&gt;&lt;p&gt;Scene 2:&lt;br /&gt;
Dim xmlDoc As New Xml.XmlDocument&lt;br /&gt;
xmlDoc.LoadXml(strXMLMsg)&lt;br /&gt;
xsn = New Xml.XmlNamespaceManager(xmlDoc.NameTable)&lt;br /&gt;
XMLNamespace = &amp;quot;http://domain.com/codename/MessagesXMLSchema.xsd&amp;quot;&lt;br /&gt;
XMLNamespacePrefix = &amp;quot;ns&amp;quot;&lt;br /&gt;
xsn.AddNamespace(XMLNamespacePrefix , XMLNamespace)&lt;br /&gt;
strXPath = &amp;quot;/ns:XMLMessage /ns:EventName&amp;quot;&lt;br /&gt;
xmlNode = xmlDoc.SelectSingleNode(strXPath,xsn)&lt;br /&gt;
strData = xmlNode.InnerText&lt;br /&gt;
In this way we will get our data parsed correctly!&lt;/p&gt;&lt;p&gt;I hope the situation mentioned above is somewhat helpful.&lt;br /&gt;
Any questions/issues/suggestions/imporvements - please mention them in your comments.&lt;/p&gt;&lt;p&gt;Have a nice time.&lt;/p&gt;&lt;p&gt;---&lt;br /&gt;&lt;b&gt;
hB&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;img src="http://v900u039rux.maximumasp.com/Web/aggbug.aspx?PostID=65" width="1" height="1"&gt;</description><category domain="http://v900u039rux.maximumasp.com/Web/blogs/hb0/archive/tags/XML/default.aspx">XML</category><category domain="http://v900u039rux.maximumasp.com/Web/blogs/hb0/archive/tags/DotNet/default.aspx">DotNet</category></item></channel></rss>