Link to home
Start Free TrialLog in
Avatar of vyotsukura
vyotsukura

asked on

Reading data from XmlNode response from a SharePoint web service

I am trying to read data from the response from the SharePoint Lists web service. My code is attached and it is pretty much straight from the MSDN library for returning list items using the SharePoint Lists web service. But when I loop through the XmlNode (nodeListItems) that presumably contains the data, on the second loop through, I get an XML stream in the innerXml string that appears to have everything that I need. What I cannot figure out from MSDN is how to properly read this data. There are 3 listItems in every one of these responses and only the second listItem has any useful data. Do I create an XmlNodeReader only on the second listItem? Am I on the right track?

I have read numerous online postings on reading XmlNodes, but I have not puzzled this one out.
string siteUrl = Properties.Settings.Default.SharePointSiteUrl;
                string libraryGUID = Properties.Settings.Default.LibraryGUID;

                SPListService.Lists spLists = new SPListService.Lists();
                spLists.Url = siteUrl + @"/_vti_bin/lists.asmx";

                spLists.Credentials = System.Net.CredentialCache.DefaultCredentials;

                // Instantiate an XmlDocument object
                System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

                string listName = libraryGUID;
                string viewName = "";
                string rowLimit = "500";

                /*Use the CreateElement method of the document object to create elements for the parameters that use XML.*/
                System.Xml.XmlNode query = xmlDoc.CreateElement("Query");

                System.Xml.XmlNode viewFields =
                    xmlDoc.CreateElement("ViewFields");

                System.Xml.XmlNode queryOptions =
                    xmlDoc.CreateElement("QueryOptions");

                query.InnerXml = "<Where><And><Eq><FieldRef Name=\"CONTRACT_NO\"/><Value Type=\"Text\">" + contractNo + "</Value></Eq>" +
                    "<Eq><FieldRef Name=\"ACTIVITYTYPE\"/><Value Type=\"int\">" + activityType + "</Value></Eq></And></Where>";

                viewFields.InnerXml = "<FieldRef Name=\"CONTRACT_NO\" />" +
                    "<FieldRef Name=\"ACTIVITY\" />" +
                    "<FieldRef Name=\"ACTIVITYDATE\" />" +
                    "<FieldRef Name=\"DOCCATEGORY\" />" +
                    "<FieldRef Name=\"DOCSTATUS\" />" +
                    "<FieldRef Name=\"DOC_TYPE\" />" +
                    "<FieldRef Name=\"RECEIVED\" />" +
                    "<FieldRef Name=\"FIRSTVIEWDATE\" />" +
                    "<FieldRef Name=\"REFNO\" />"; 

                queryOptions.InnerXml = "";

                System.Xml.XmlNode nodeListItems = spLists.GetListItems
                    (listName, viewName, query, viewFields, rowLimit, queryOptions, null);

                foreach (System.Xml.XmlNode listItem in nodeListItems)
                {
                    string innerXml = listItem.InnerXml;
                    docCount++;
                }

Open in new window

Avatar of vyotsukura
vyotsukura

ASKER

If I read the OuterXml property of each of these 3 XmlNodes in the web service response, the first and last values contain only the "/n" tag, while the second value contains the entire XML stream for all of the data that I need. I can tell where each useful element of this XML stream begins and ends, but I cannot seem to get the XMLReader to read the data in a useful way.
Sorry, the tag in the first and third nodes looks like this: \n
I found a partial solution to this problem. By using the SelectNodes method on the XmlNode object (see attached code), I was able to pare down the XML response to just the specific records in the SharePoint Library that I want to examine. Following the execution of this method, I have 6 XmlNodes in the nodeList, each with 23 attributes, which I can see in the Non-Public members of the Attributes property. How can I get to these attributes?

I have tried iterating through these 23 attributes with an XmlNodeReader, but all of the attributes are empty and neither the GetAttribute method on the XmlNodeReader nor the Attributes[string name].Value approach get me the necessary data.

What am I doing wrong?
string xPath = "//*[local-name() = 'data' and namespace-uri() = 'urn:schemas-microsoft-com:rowset']/*[local-name() = 'row' and namespace-uri() = '#RowsetSchema']";

                XmlNodeList nodeList = nodeListItems.SelectNodes(xPath);

Open in new window

Avatar of Ted Bouskill
Looking at your code compared to examples like this:
http://blogs.msdn.com/sharepoint/archive/2007/03/25/retrieving-sharepoint-list-items-attachment-urls-using-lists-web-service.aspx
it appears you might be running into syntax issues.

Have you searched the web for articles on consuming Sharepoint web services?  There are some great sites with excellent samples
Thank you for the suggestion - I will take a look! I have searched for articles on reading XmlNodes as they relate to SharePoint web services but I will modify my search a bit and see what I find.
ASKER CERTIFIED SOLUTION
Avatar of vyotsukura
vyotsukura

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