Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 463
  • Last Modified:

Help needed XPath.

Hi,

I'm nearly getting crazy at the moment about a really simple problem.

I use the GetUserCollectionFromWeb() Soap request to get some user data from a sharepoint server. It is giving me back some xml data. This looks like:

<GetUserCollectionFromWeb xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">
  <Users>
    <User ID="1" Sid="S-1-5-21-8395456415-1532298954-1801674531-29583" Name="Mustermann Michael" LoginName="mydom\mustermann" Email="Mustermann.Michael@mydom.com" Notes="" IsSiteAdmin="False" IsDomainGroup="False" />
    <User ID="1073741823" Sid="S-1-0-0" Name="System Account" LoginName="SHAREPOINT\system" Email="" Notes="" IsSiteAdmin="False" IsDomainGroup="False" />
  </Users>
</GetUserCollectionFromWeb>

Open in new window


I need now a list with all single user to get the data from it and write it to a table in the database.

I tried the following two options. Both are not returning a node:
  XElement ndListItems = userGroupSoapClient.GetUserCollectionFromSite();
            
            object test1 = ndListItems.Descendants("User").ToList();

            List<XElement> test = ndListItems.XPathSelectElements("//User").ToList();

Open in new window


Could please someone assist?

Thanks in advance
0
HugoHiasl
Asked:
HugoHiasl
  • 3
  • 2
1 Solution
 
käµfm³d 👽Commented:
Your Descendants call refers to "User" where I think you meant to refer to "Users" (plural).
0
 
HugoHiaslAuthor Commented:
This does not change anything.

But I would have expected a list with 1 resultNode with 6 subnodes if I use "Users"  and I would expect a list with 6 nodes if I use "User".

But both gives 0 entries back. Does it have probably to do with the xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/" entry  ??
0
 
käµfm³d 👽Commented:
Actually, disregard my previous comment...  I incorrectly read what you were trying to do.

The basic issue here is that your XML has a namespace and you are not accounting for it in your document inspections. For your first attempt (Descendants), you can include an XNamespace in your call:

XNamespace ns = ndListItems.GetDefaultNamespace();

object test1 = ndListItems.Descendants(ns + "User").ToList();

Open in new window


I'm still working out the second example. I'll post back with an update.
0
 
HugoHiaslAuthor Commented:
This did not work because the DefaultNamespace seems to be empty...

But there is a Property "Name" which has a Subproperty Namespace which returns a XNamespace.

With this it works.

Thanks for your input  It pointed me to the right direction.
0
 
käµfm³d 👽Commented:
This did not work because the DefaultNamespace seems to be empty...
Hmm...  My test was to put your XML in a file and load it into an XElement with XElement.Load. Perhaps there is something different in the way the default namespace is handled between our two codes.

In any event, combining your method with my method, here is the promised code for your second example:
System.Xml.XmlNamespaceManager mgr = new System.Xml.XmlNamespaceManager(ndListItems.CreateReader().NameTable);

mgr.AddNamespace("xyz", ndListItems.Name.Namespace.NamespaceName);

List<XElement> test = ndListItems.XPathSelectElements("//xyz:User", mgr).ToList();

Open in new window

0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now