tags:

views:

39

answers:

2

I'm testing the SelectSingleNode function to fetch a single node from an XMLNode object in Visual Studio as follows:

        Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xmlDoc.NameTable)
        nsmgr.AddNamespace(ndListItems.Prefix, ndListItems.NamespaceURI)
        Dim dummy As XmlNode = ndListItems.SelectSingleNode("/listitems", nsmgr)

At this point, I'm just trying to get the root node and I'm using the prefix and namespaceURI property to add to the XmlNamespaceManager . Problem is that when I run the debugger the dummy variable is not being assigned, i.e it is Nothing. Just to note, when I analyze the values of the Prefix and namespace property they are as follows, Prefix="" and NamespaceURI="http://schemas.microsoft.com/sharepoint/soap"

UPDATE:

Tried changing the code, but my dummy XMLNode is still not getting set

Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xmlDoc.NameTable)
        nsmgr.AddNamespace(ndListItems.Prefix, ndListItems.NamespaceURI)
        Dim dummy As XmlNode = ndListItems.SelectSingleNode("/" + ndListItems.Prefix + "listitems", nsmgr)

Here is the XML code snippet of what I'm trying to get, my ultimate goal is to access the attributes of the z:row node

<listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" xmlns="http://schemas.microsoft.com/sharepoint/soap/"&gt;
<rs:data ItemCount="1">
   <z:row ows_Title="Newhire" ows_FirstName="Rick" ows_WorkPhone="954" ows_HomePhone="954" ows_Email="[email protected]" ows_UserID="Rick.Newhire" ows_MetaInfo="9;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="9" ows_owshiddenversion="1" ows_UniqueId="9;#{0F6251A9-D3B8-4B07-A5F8-23BAF5F2237E}" ows_FSObjType="9;#0" ows_Created="2010-08-18 15:56:40" ows_FileRef="9;#Lists/NewHires/9_.000" />
</rs:data>
</listitems>
A: 

You obviously have problem adding namespaces to the namespace manager.

Also, your XPath expression will at best select the top element, however you want z:row.

One simple and not too-elegant way to avoid namespace processing is the following

Use:

SelectSingleNode("/*/*/*")

or

SelectSingleNode("/*/*[local-name()='data']/*[local-name()='row']")
Dimitre Novatchev
Hey Dimitre, thanks for getting back to me. I tried the above snippet of code, but I'm still getting nothing in my dummy variable. I posted the code above and the XML snippet as Strelok suggested
kingrichard2005
@kingrichard2005: I completely re-wrote my solution to one that doesn't use namespaces at all. You have to learn more about working with namespaces, but this topic is beyond being explained in a single question.
Dimitre Novatchev
Still not getting assigned. I found an article on XML Namespaces on MSDN that I hope will clarify things. Not sure why it's still not getting assigned, I tried both of your solutions.
kingrichard2005
@kingrichard2005: I can assure you that these expressions select the desired nodes -- as XPath is concerned these are OK -- there must be something weird in the c# code.
Dimitre Novatchev
A: 

In your input sample the QName tuple for z:row element is ("#RowsetSchema","row","z"). Meaning that the namespace URI for z prefix is #RowsetSchema.

If I'm not getting wrong your C# code, this ndListItems.Prefix evaluate to listitems element's prefix wich is none or "". So, when you say "/" + ndListItems.Prefix + "listitems", it gets evaluated to "/listitems" wich it will be interpreted as a listitems under no namespace.

So, I think you need:

Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xmlDoc.NameTable) 
nsmgr.AddNamespace("soap", "http://schemas.microsoft.com/sharepoint/soap/")
nsmgr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset")
nsmgr.AddNamespace("z", "#RowsetSchema")
Dim dummy As XmlNode = ndListItems.SelectSingleNode("/soap:listitems/rs:data/z:row", nsmgr)

Edit: It came to me, after I had posted.

Alejandro