Solved

Querying with XPath in C#

Posted on 2009-07-15
10
1,149 Views
Last Modified: 2013-11-11
Hi all,

I am trying to connect to a Sharepoint list and extract data from it with the GetListItems method into a XmlNode.  I am able to get as far as connecting to the Sharepoint List and extract the list items.  But it comes back in an XML fragment like this:

<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/">
   <rs:data ItemCount="4">
      <z:row ows_Number_Field="6555.00000000000"
         ows_Created="2003-06-18T03:41:09Z"
         ows_ID="3" ows_owshiddenversion="3" />
      <z:row ows_Number_Field="78905456.0000000"
         ows_Created="2003-06-18T17:15:58Z"
         ows_ID="4" ows_owshiddenversion="2" />
         ...
   </rs:data>
</listitems>

Is there a way to run an Xpath query to filter out just the information in:
z:row ows_Number_Field="78905456.0000000"

Thanks in advance.

pwit
0
Comment
Question by:Pwit
  • 6
  • 3
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24864810
I imagine your XPath should be something like

    //listitems/rs:data/z:row[@ows_Number_Field='78905456.0000000']
0
 

Author Comment

by:Pwit
ID: 24865478
Hi Kaufmed,

Thanks for the quick reply, but when I build and try to get the run the program I get this error:

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

I tried to look this up at MSDN, but this was all I could find:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:newbooks-schema");

Do I have to apply this to my function or is there something I am missing?  Thanks again.

pwit
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24865785
I'm not completely sure of the "best practice" way to do it, but when I had something similar, I did have to add the namesapce manager and namespaces to the reader.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 23

Assisted Solution

by:Tony McCreath
Tony McCreath earned 150 total points
ID: 24868246
Yep, you need to handle the namespaces:

http://support.microsoft.com/kb/318545
0
 

Author Comment

by:Pwit
ID: 24871317
Hi guys,

I been tinkering around with the namespace stuff all morning, but haven't been able to get my code to generate anything useful.  Here is the xml fragment I been able to generate and afterwards convert to an xmldocument.  But I am still clueless on how to assign the namespaces and query out something useful, like the ows_title="Test tile" in the xml below:

<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/">
<rs:data ItemCount="2">
<z:row ows_Title="Test tile" ows_Message="test Message" ows_MetaInfo="1;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="1" ows_owshiddenversion="2" ows_UniqueId="1;#{E59FDB30-85AD-462E-B921-A16AAED789C6}" ows_FSObjType="1;#0" ows_Created="2009-07-13 14:31:22" ows_FileRef="1;#sites/support/it/development/Lists/TargetList/1_.000" />
<z:row ows_Title="pwit" ows_Message="man" ows_MetaInfo="2;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="2" ows_owshiddenversion="1" ows_UniqueId="2;#{DF0FD21B-1A9A-4038-8829-C37697A80898}" ows_FSObjType="2;#0" ows_Created="2009-07-13 14:41:28" ows_FileRef="2;#sites/support/it/development/Lists/TargetList/2_.000" />
</rs:data>
</listitems>


Also attached is the code that I have so far.  Thanks for your help.

pwit
private void takeListItem()
        {
            chi02devmoss.Lists listService = new chi02devmoss.Lists();
            listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
 
            String listName = "TargetList";
            XmlDocument xmlDoc = new XmlDocument();
 
            XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
            XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
            XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
 
            ndQueryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" + "<DateInUtc>FALSE</DateInUtc>";
            ndViewFields.InnerXml = "<FieldRef Name='Title' /> <FieldRef Name='Message'/>";
            //ndQuery.InnerXml = "<Where><And><Gt><FieldRef Name='Title'/>" + "<Value Type='Number'>5000</Value></Gt><Gt><FieldRef Name='Message'/>" + "<Value Type = 'DateTime'>2003-07-03T00:00:00</Value></Gt></And></Where>";
 
            try
            {
                XmlNode ndListItems = listService.GetListItems(listName, null, ndQuery, ndViewFields, null, ndQueryOptions, null);
                //MessageBox.Show(ndListItems.OuterXml);
                XmlDocument x = new XmlDocument();
 
                x.LoadXml(ndListItems.OuterXml);
                System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(x.NameTable);
 
                xmlnsManager.AddNamespace("first", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
                xmlnsManager.AddNamespace("second", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");
                xmlnsManager.AddNamespace("third", "urn:schemas-microsoft-com:rowset");
 
                System.Xml.XmlNodeList test;
                test = x.SelectNodes("//first:row[@ows_Title]", xmlnsManager);
 
                foreach (XmlNode t in test)
                {
                    MessageBox.Show(t.InnerXml);
                }
 
            }
 
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                MessageBox.Show("Message:\n" + ex.Message + "\nDetail:\n" + ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace);
            }
 
        }

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24871389
Where you have "first," "second" and "third," you should have the prefix that is associated with the uid. Something like:
xmlnsManager.AddNamespace("s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
xmlnsManager.AddNamespace("dt", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");
xmlnsManager.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24871409
Then your XPath would be:
test = x.SelectNodes("z:row[@ows_Title]", xmlnsManager);

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 350 total points
ID: 24871421
Addition to previous post.
xmlnsManager.AddNamespace("s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
xmlnsManager.AddNamespace("dt", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");
xmlnsManager.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
xmlnsManager.AddNamespace("z", "#RowsetSchema");

Open in new window

0
 

Author Comment

by:Pwit
ID: 24872918
Thanks kaufmed, those namespaces worked like a charm and after changing the xpath to "//z:row", everything started working.  Thanks again for all your help!
XmlNode ndListItems = listService.GetListItems(listName, null, ndQuery, ndViewFields, null, ndQueryOptions, null);
                
                System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xmlDoc.NameTable);
 
               xmlnsManager.AddNamespace("s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
xmlnsManager.AddNamespace("dt", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");
xmlnsManager.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
xmlnsManager.AddNamespace("z", "#RowsetSchema");
 
                System.Xml.XmlNodeList test;
                test = x.SelectNodes("//z:row", xmlnsManager);
 
                foreach (XmlNode t in test)
                {
                    MessageBox.Show(t.Attributes["ows_Title"].InnerText);
                }
 

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24873252
Cool. I'm still kind of noob-ish when it comes to XML so I wasn't completely sure about the "//". But glad to help nonetheless :)
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question