Link to home
Start Free TrialLog in
Avatar of HugoHiasl
HugoHiasl

asked on

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

Your Descendants call refers to "User" where I think you meant to refer to "Users" (plural).
Avatar of HugoHiasl
HugoHiasl

ASKER

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  ??
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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