{"id":481,"date":"2012-01-17T12:54:08","date_gmt":"2012-01-17T11:54:08","guid":{"rendered":"http:\/\/blog.repsaj.nl\/?p=481"},"modified":"2012-01-17T12:54:08","modified_gmt":"2012-01-17T11:54:08","slug":"namespace-problems-querying-with-xelement","status":"publish","type":"post","link":"http:\/\/blog.repsaj.nl\/index.php\/2012\/01\/namespace-problems-querying-with-xelement\/","title":{"rendered":"Namespace problems querying with XElement"},"content":{"rendered":"<p>Today I was working on\u00a0an old SharePoint 2007 project which heavily uses the SharePoint webservices. That implies using a lot of XML as input and output for those services. At one point I had to extract some info from the XML fragment being returned.<\/p>\n<p>The function returned an XElement. This type of object can be queried via Linq, or you can use the Element \/ Elements methods to get elements from a node. My XML was structured like this:<\/p>\n<p><code>&lt;Results xmlns=\"http:\/\/schemas.microsoft.com\/sharepoint\/soap\/\"&gt;<br \/>\n&lt;Result ID=\"1,Update\"&gt;<br \/>\n&lt;ErrorCode&gt;0x00000000&lt;\/ErrorCode&gt;<br \/>\n&lt;ErrorText&gt;Some text&lt;\/ErrorCode&gt;<br \/>\n&lt;\/Result&gt;<br \/>\n&lt;\/Results&gt;<\/code><\/p>\n<p>This was all being returned as a XElement variable which I named &#8220;resultNode&#8221;. I wanted to fetch the ErrorText value, so I tried things like:<br \/>\n<code><br \/>\nresultNode.Element(\"Result\").Element(\"ErrorText\")<br \/>\nresultNode.Elements(\"Result\").First().Elements(\"ErrorText\").First()<\/code><\/p>\n<p>To cut a long story short; this all wasn&#8217;t working. The problem is caused by the document having a different default\u00a0namespace (i.e. <a href=\"http:\/\/schemas.microsoft.com\/sharepoint\/soap\/\">http:\/\/schemas.microsoft.com\/sharepoint\/soap\/<\/a>). This means all elements become prefixed with this namespace and you need to include it in your queries. You can do this like this:<br \/>\n<code><br \/>\nresultNode.Element(\"Result\") becomes:<br \/>\nresultNode.Element(resultNode.Name.Namespace + \"Result\");<\/code><\/p>\n<p>There are probably ways to  set the default namespace, or to remove the namespace completely so it doesn&#8217;t bother you (just make sure there aren&#8217;t multiple namespaces defined). But for simple things, this fixes the problem neatly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I was working on\u00a0an old SharePoint 2007 project which heavily uses the SharePoint webservices. That implies using a lot of XML as input and output for those services. At one point I had to extract some info from the XML fragment being returned. The function returned an XElement. This type of object can be<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[34],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3KFR1-7L","_links":{"self":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/posts\/481"}],"collection":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/comments?post=481"}],"version-history":[{"count":0,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/posts\/481\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/media?parent=481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/categories?post=481"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/tags?post=481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}