Skip to main content
 首页 » 编程设计

c#之如何解析从 SharePoint Web 服务返回的内部 XML

2024年02月23日29think

我有这样的程序

    // 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 对象存在。

  1. 您究竟需要什么样的“解析”?
  2. 您在哪里看到任何 HTML?我在这里看到的只是 XML。
  3. 如果您使用的是 SQL Server 2005 或更高版本,则可以将数据存储在“XML”类型的列中。