其他分享
首页 > 其他分享> > CodeGo.net>有什么容易的浪费将XML输出从sharepoint GetListItems()转换为DataTable

CodeGo.net>有什么容易的浪费将XML输出从sharepoint GetListItems()转换为DataTable

作者:互联网

我能够从共享点检索数据

 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);

我现在拥有一个包含所有数据的巨大XML博客.有没有简单的方法可以将其转换为数据表,所以我将其视为矩阵来遍历每一行?

我尝试过类似的方法,但似乎不起作用:

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

解决方法:

嗯,从GetListItems返回的xml与SPListItemCollection.Xml相同.我有一个扩展方法,可以将此XML转换为数据表.

您可以尝试在返回的XML上使用ConvertZRowToRegularXml方法-您将获得DataTable可以理解的XML.

Vincent Rothwell获得了the solution的积分.

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;
            }
        }
    }   

标签:datatable,xmlnode,c,sharepoint
来源: https://codeday.me/bug/20191209/2097879.html