is there any easy wasy to convert the XML output from sharepoint GetListItems() to a DataTable

六月ゝ 毕业季﹏ 提交于 2019-12-23 19:57:26

问题


i am able to retrieve data from sharepoint

 com.sharepoint2.Lists lists = new Lists();
 lists.Credentials = new System.Net.NetworkCredential("user", "pwd", "domain");
 lists.Url = "http://sharepoint2.company.com/sites/mysite/_vti_bin/Lists.asmx";

XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
XmlNode listitems = lists.GetListItems("MyList", null, ndQuery, ndViewFields, null,ndQueryOptions, null);

i now have this huge XML blog with all of my data. Is there any easy way to convert this to a datatable so i treat this like a matrix to loop through each row ??

i tried something like this but it doesn't seem to work:

System.IO.StringReader sr = new System.IO.StringReader(listitems.OuterXml);
XmlTextReader tr = new XmlTextReader(sr);
DataSet ds = new DataSet("resultDataSet");
ds.ReadXml(tr);

回答1:


Ahh, xml returned from GetListItems is almos the same as SPListItemCollection.Xml. I have an extension method that converts this XML into datatable.

You can just try to use ConvertZRowToRegularXml method on your returned XML - and you would get back an XML that DataTable understands.

Credits for the solution goes to Vincent Rothwell.

public static class SPListItemCollectionExtensions
    {
        public static readonly string xsltFromZRowToXml =
                "<xsl:stylesheet version=\"1.0\" " +
                 "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" " +
                 "xmlns:s=\"uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882\" " +
                 "xmlns:z=\"#RowsetSchema\">" +
             "<s:Schema id=\"RowsetSchema\"/>" +
             "<xsl:output method=\"xml\" omit-xml-declaration=\"yes\" />" +
             "<xsl:template match=\"/\">" +
              "<xsl:text disable-output-escaping=\"yes\">&lt;rows&gt;</xsl:text>" +
              "<xsl:apply-templates select=\"//z:row\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">&lt;/rows&gt;</xsl:text>" +
             "</xsl:template>" +
             "<xsl:template match=\"z:row\">" +
              "<xsl:text disable-output-escaping=\"yes\">&lt;row&gt;</xsl:text>" +
              "<xsl:apply-templates select=\"@*\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">&lt;/row&gt;</xsl:text>" +
             "</xsl:template>" +
             "<xsl:template match=\"@*\">" +
              "<xsl:text disable-output-escaping=\"yes\">&lt;</xsl:text>" +
              "<xsl:value-of select=\"substring-after(name(), 'ows_')\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">&gt;</xsl:text>" +
              "<xsl:value-of select=\".\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">&lt;/</xsl:text>" +
              "<xsl:value-of select=\"substring-after(name(), 'ows_')\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">&gt;</xsl:text>" +
             "</xsl:template>" +
            "</xsl:stylesheet>";

        public static DataTable GetFullDataTable(this SPListItemCollection itemCollection)
        {
            DataSet ds = new DataSet();

            string xmlData = ConvertZRowToRegularXml(itemCollection.Xml);
            if (string.IsNullOrEmpty(xmlData))
                return null;

            using (System.IO.StringReader sr = new System.IO.StringReader(xmlData))
            {
                ds.ReadXml(sr, XmlReadMode.Auto);

                if (ds.Tables.Count == 0)
                    return null;

                return ds.Tables[0];
            }
        }

        static string ConvertZRowToRegularXml(string zRowData)
        {
            XslCompiledTransform transform = new XslCompiledTransform();
            XmlDocument tidyXsl = new XmlDocument();

            try
            {
                //Transformer
                tidyXsl.LoadXml(Balticovo.SharePoint.Extensions. SPListItemCollectionExtensions.xsltFromZRowToXml);
                transform.Load(tidyXsl);

                //output (result) writers
                using (System.IO.StringWriter sw = new System.IO.StringWriter())
                {
                    using (XmlTextWriter tw = new XmlTextWriter(sw))
                    {
                        //Source (input) readers
                        using (System.IO.StringReader srZRow = new System.IO.StringReader(zRowData))
                        {
                            using (XmlTextReader xtrZRow = new XmlTextReader(srZRow))
                            {
                                //Transform
                                transform.Transform(xtrZRow, null, tw);
                                return sw.ToString();
                            }
                        }
                    }
                }
            }
            catch
            {
                return null;
            }
        }
    }   



回答2:


You should pass the XmlNodeType to the XmlTextReader like this:

DataSet ds=new DataSet();
using(var reader=new XmlTextReader(listItems.OuterXml,XmlNodeType.Element,null))
{
    ds.ReadXml(reader);
}

Concering the object model vs the web services: The usual way to create web parts, event receivers etc. is to use Sharepoint's server object model. The web services are not used that much, because they are a bit cumbersome. In Sharepoint 2010 a new client object model and the WCF Data Services makes things a LOT easier and the old ASMX web services are considered deprecated.

BUT Sharepoint Online is actually a 2007 version that doesn't allow server-side code, so AJAX and the ASMX web services are the only way to customize and communicate with a Sharepoint Online site. This has brought the ASMX services to the front just as they became deprecated. Go figure.



来源:https://stackoverflow.com/questions/3231688/is-there-any-easy-wasy-to-convert-the-xml-output-from-sharepoint-getlistitems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!