Search code examples
c#xmlsharepoint-2010datasetxmldocument

XmlDocument to DataSet only returning 1 Row


I have an problematic piece of code and it's something really peculiar and nothing I've ever experienced before!

I'm calling a Sharepoint SOAP function and the results are being returned absolutely fine., many XML records of data are being retruned.

Now I have tried to convert the results into an XmlDocument, which I then use to load into a DataSet.

However when it gets inserted into the DataSet it's only inserting 1 record, which happens to be the very first record of the Xml.

The problematic code is the below:

            Lists list = new Lists();               
            list.Url = URL + "_vti_bin/Lists.asmx";
            list.UseDefaultCredentials = true;

            //Gets the entire Lists attached to the Sharepoint Site
            XmlNode Results = list.GetListCollection();

           //Writes the entire results into an XmlDocument.
            doc.AppendChild(doc.ImportNode(Results, true));


            using (StringReader xmlSR = new StringReader(doc.InnerXml))
            {
                ds.ReadXml(xmlSR, XmlReadMode.Auto);
            }

The Xml from 'doc.InnerXml' is all valid and is pastable into Xml Notepad 2007, so i'm a bit at a lost.

I hope someone can shed some light onto this, be much appreciated


Solution

  • The following example works for me:

    Lists list = new Lists(); //SharePoint Lists SOAP service
    
    //Perform request
    XmlNode result = list.GetListCollection();
    
    
    //Process result
    var ds = new DataSet("ListsResults");
    using (var reader = new StringReader(result.OuterXml))
    {
        ds.ReadXml(reader, XmlReadMode.Auto);
    }
    
    //print List Titles
    foreach (DataRow row in ds.Tables[0].Rows)
    {
         Console.WriteLine(row["Title"]);
    }
    

    Another common approach is to utilize LINQ to XML:

    Lists list = new Lists(); //SharePoint Lists SOAP service
    
    //Perform request
    XmlNode result = list.GetListCollection();
    
    
    var docResult = XDocument.Parse(result.OuterXml);
    XNamespace s = "http://schemas.microsoft.com/sharepoint/soap/";
    var listEntries = from le in docResult.Descendants(s + "List")
                              select new
                                  {
                                      Title = le.Attribute("Title").Value
                                  };
    foreach (var e in listEntries)
    {
         Console.WriteLine(e.Title);
    }