0

I'm currently writing a C# class that allows me to construct entities from an OData feed. Don't ask why, I just need it at the moment :)

The snippet of the XML looks like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xml:base="http://demo.tenforce.acc/Api.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <id>http://demo.tenforce.acc/Api.svc/Items(387)</id>
  <title type="text"></title>
  <updated>2011-09-22T07:35:54Z</updated>
  <author>
    <name />
  </author>
  <link rel="edit" title="Item" href="Items(387)" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Children" type="application/atom+xml;type=feed" title="Children" href="Items(387)/Children" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Parent" type="application/atom+xml;type=entry" title="Parent" href="Items(387)/Parent" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="Items(387)/Attachments" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Predecessors" type="application/atom+xml;type=feed" title="Predecessors" href="Items(387)/Predecessors" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Successors" type="application/atom+xml;type=feed" title="Successors" href="Items(387)/Successors" />
  <category term="TenForce.Execution.Api2.Objects.Item" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <content type="application/xml">
    <m:properties>
      <d:Id m:type="Edm.Int32">387</d:Id>
    </m:properties>
  </content>
</entry>

I've created a code snippet that loads the entire xml string into an XmlDocument, and generates a XmlNamespaceManager as well to have access to the various namespaces.

I'm trying to select the <category> element from the XML but I can't seem to get the Xpath expression right. I've tried the following:

  • //entry/category
  • descendant::xmlns:cateogry
  • //d:category
  • //m:category
  • //category
  • //xml:category

But none seem to be selected the node in question.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
codingbunny
  • 3,989
  • 6
  • 29
  • 54
  • possible duplicate of [Selecting nodes with the default namespace](http://stackoverflow.com/questions/1008930/selecting-nodes-with-the-default-namespace) – Dimitre Novatchev Sep 22 '11 at 13:18
  • another duplicate [Using Xpath With Default Namespace in C#](http://stackoverflow.com/questions/585812/using-xpath-with-default-namespace-in-c) – Jonathan Dickinson Sep 22 '11 at 13:37

1 Answers1

0

Oh never mind, i've found the solution. I had to add the namespace for the atom elements....

var manager = new XmlNamespaceManager(_mDocument.NameTable);
manager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
manager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
manager.AddNamespace("atom", "http://www.w3.org/2005/Atom");

This allowed me to select the <category> element by using //atom:category

codingbunny
  • 3,989
  • 6
  • 29
  • 54