Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何解析来自SharePoint Web Services的内部XML返回_C#_.net_Xml_Sharepoint - Fatal编程技术网

C# 如何解析来自SharePoint Web Services的内部XML返回

C# 如何解析来自SharePoint Web Services的内部XML返回,c#,.net,xml,sharepoint,C#,.net,Xml,Sharepoint,我有一个这样的计划 // establish proxy obj SPLists.Lists listservice = new SPLists.Lists(); // credentials listservice.PreAuthenticate = true; // user name Console.Write("Username (e.g. bobdole@xyz.com): "); string usrname = Cons

我有一个这样的计划

    // establish proxy obj
    SPLists.Lists listservice = new SPLists.Lists();

    // credentials
    listservice.PreAuthenticate = true;


    // user name
    Console.Write("Username (e.g. bobdole@xyz.com): ");
    string usrname = Console.ReadLine().Trim();
    Console.Write("\n");

    // pw
    Console.Write("Password: ");
    string password = MaskedConsoleReader.ReadLine();

    // auth
    listservice.Credentials = new NetworkCredential(usrname, password);

    // List Service URL
    listservice.Url =
      "https://wss.xyz.com/_vti_bin/Lists.asmx";

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


    /* Assign values to the string parameters of the GetListItems method, 
    using GUIDs for the listName and viewName variables. For listName, 
    using the list display name will also work, but using the list GUID 
    is recommended. For viewName, only the view GUID can be used. 
    Using an empty string for viewName causes the default view to be used.*/
    // Work ticket list
    // {5B79ED7D-ECB7-447A-9331-22984E52EB7D}
    // All tickets view
    // {00F95DDD-C383-4E4A-94F2-977FDA7A7F74}

    string listName = "{5B79ED7D-ECB7-447A-9331-22984E52EB7D}";
    string viewName = "{00F95DDD-C383-4E4A-94F2-977FDA7A7F74}";
    string rowLimit = "4000";

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

    /*To specify values for the parameter elements (optional), assign 
    CAML fragments to the InnerXml property of each element.*/
    query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" +
        "<Value Type=\"Counter\">3</Value></Gt></Where>";
    viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
    queryOptions.InnerXml = "";

    /* Declare an XmlNode object and initialize it with the XML response 
    from the GetListItems method. The last parameter specifies the GUID 
    of the Web site containing the list. Setting it to null causes the 
    Web site specified by the Url property to be used.*/

    DateTime abc = DateTime.Now;
    string format = "yyyy-M-dd_HH-MM-ss";
    string xyz = abc.ToString(format);
    string fName = "dump-" + xyz + ".txt";

    try
    {

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

        StreamWriter outfile = new StreamWriter(fName);


        /*Loop through each node in the XML response and display each item.*/
        foreach (System.Xml.XmlNode listItem in nodeListItems)
        {
            Console.WriteLine(listItem.OuterXml + "\n");
            outfile.WriteLine(listItem.OuterXml);

        }
        outfile.Close();


    }
    catch (Exception ex)
    {
        Console.WriteLine("ERROR!: " + ex.ToString());
    }

    Console.WriteLine("\n");
    Console.WriteLine("Contents dumped to: " + fName);

    // hold program
    Console.ReadLine();

}
它从listItem.OuterXML生成一个字符串:

<rs:data ItemCount="896" xmlns:rs="urn:schemas-microsoft-com:rowset">
[...]
<z:row ows_Title="Do A B AND C" ows_ProjectID="1165;#_INTERNAL" ows__ModerationStatus="0" ows__Level="1" ows_Deliverables="&lt;div&gt;Implementation of blah blah blah" ows_AssignedTo="7;#blah@xyz.com" ows_ID="5" ows_owshiddenversion="29" ows_UniqueId="5;#{DD129C47-C9E0-4962-A516-B8280EB77800}" ows_FSObjType="5;#0" ows_Created="2009-02-27 08:25:28" ows_Category="7 Internal Project" ows_Comment="&lt;div&gt;Changing to Queue of 22 blah can do blah" ows_Priority="4 Low" ows_Status="1 In Queue" ows_Description_x0020_of_x0020_Work="do blah and blah" ows_FileRef="5;#Lists/Work Tick/5_.000" ows_ProjectOwner="7;#blah@xyz.com" ows_MetaInfo="5;#" xmlns:z="#RowsetSchema" />
[...]
</rs:data>

在返回的字符串中大约有800行这样的代码。解析每行数据的最简单方法是什么?如果可能的话,我还想去掉HTML,因为我希望最终使用SQLClient名称空间将其转储到SQL server中。

如果没有使用OuterXml将数据转换为字符串,就不需要解析数据。解析后的数据已经作为XmlNode对象存在

那么您需要什么样的解析呢? 你在哪里看到HTML?我在这里看到的只是XML。 如果您使用的是SQL Server 2005或更高版本,则可以将数据存储在XML类型的列中。
如果没有使用OuterXml将数据转换为字符串,就没有必要解析数据。解析后的数据已经作为XmlNode对象存在

那么您需要什么样的解析呢? 你在哪里看到HTML?我在这里看到的只是XML。 如果您使用的是SQL Server 2005或更高版本,则可以将数据存储在XML类型的列中。
使用SharePoint 2010?相反,使用客户端对象模型,将数据传送到SQL数据访问层将非常容易。使用SharePoint 2010?改用客户机对象模型,将数据传送到您的SQL数据访问层将非常容易。@James:谢谢,但他已经有XmlNode实例了。我只是建议他使用它们。确切地说,在我注意到之前,我是在给你投赞成票,因为你把它贴了出来:P@James:谢谢,但他已经有XmlNode实例了。我只是建议他使用它们。确切地说,在我注意到之前,我是在给你投赞成票