【发布时间】:2011-02-22 02:45:21
【问题描述】:
我有一个这样的程序
// 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="<div>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="<div>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 服务器中。
【问题讨论】:
-
使用 SharePoint 2010?改用客户端对象模型,将数据穿梭到 SQL 数据访问层会容易得多。
标签: c# .net xml sharepoint