我可以将此值“导入”XML结构并解析它吗? C#

时间:2009-08-12 20:20:32

标签: c# xml sharepoint

我有一个sharepoint服务器,我正在以编程方式使用它。我正在玩的其中一个应用是Microsoft's Call Center。我查询了客户名单:

if (cList.Title.ToLower().Equals("service requests"))
{
    textBox1.Text += "> Service Requests" + Environment.NewLine;
    foreach (SPListItem item in cList.Items)
    {
        textBox1.Text += string.Format(">> {0}{1}", item.Title, Environment.NewLine);
    }
}

item中的一个属性是XML。这是一个值:

<z:row
  xmlns:z='#RowsetSchema' ows_ID='1' ows_ContentTypeId='0x0106006324F8B638865542BE98AD18210EB6F4'
  ows_ContentType='Contact' ows_Title='Mouse' ows_Modified='2009-08-12 14:53:50' ows_Created='2009-08-12 14:53:50'
  ows_Author='1073741823;#System Account' ows_Editor='1073741823;#System Account'
  ows_owshiddenversion='1' ows_WorkflowVersion='1' ows__UIVersion='512' ows__UIVersionString='1.0'
  ows_Attachments='0' ows__ModerationStatus='0' ows_LinkTitleNoMenu='Mouse' ows_LinkTitle='Mouse'
  ows_SelectTitle='1' ows_Order='100.000000000000' ows_GUID='{37A91B6B-B645-446A-8E8D-DA8250635DE1}'
  ows_FileRef='1;#Lists/customersList/1_.000' ows_FileDirRef='1;#Lists/customersList'
  ows_Last_x0020_Modified='1;#2009-08-12 14:53:50' ows_Created_x0020_Date='1;#2009-08-12 14:53:50'
  ows_FSObjType='1;#0' ows_PermMask='0x7fffffffffffffff' ows_FileLeafRef='1;#1_.000'
  ows_UniqueId='1;#{28A223E0-100D-49A6-99DA-7947CFC38B18}' ows_ProgId='1;#'
  ows_ScopeId='1;#{79BF21FE-0B9A-43B1-9077-C071B61F5588}' ows__EditMenuTableStart='1_.000'
  ows__EditMenuTableEnd='1' ows_LinkFilenameNoMenu='1_.000' ows_LinkFilename='1_.000'
  ows_ServerUrl='/Lists/customersList/1_.000' ows_EncodedAbsUrl='http://spvm:3333/Lists/customersList/1_.000'
  ows_BaseName='1_' ows_MetaInfo='1;#' ows__Level='1' ows__IsCurrentVersion='1' ows_FirstName='Mickey'
  ows_FullName='Mickey Mouse' ows_Comments='&lt;div&gt;&lt;/div&gt;' ows_ServerRedirected='0'
/>

我可以创建XMLnode或其他类型的XML对象,以便我可以轻松地解析它并提取某些值(这些确定性现在是未知数,因为我现在只是测试)?

非常感谢!

3 个答案:

答案 0 :(得分:4)

如果XML有效,您可以像这样使用XmlDocument.LoadXMl:

XmlDocument doc = new XmlDocument();
doc.LoadXml(validxmlstring);

答案 1 :(得分:1)

你可以这样做,它应该工作正常(虽然我会使用XML文档方法Colin mentions,甚至更好的LINQ)。您还可以在SharePoint Extensions Lib中找到LINQ扩展名。

但是,我想知道为什么你会这样做而不是使用SPListItem.Item属性?使用起来非常简单,非常清晰。例如:

var title = listItem["Title"];       // Returns title of item
var desc = listItem["Description"];  // Returns value of description field

唯一的陷阱是列表的异常情况,该列表包含内部名称等于另一个字段的显示名称的字段。这将始终返回具有内部名称的字段的值。

如果您需要使用XML路线,那就太好了。

答案 2 :(得分:0)

我想出了这个似乎工作正常的代码。由于我的XML / C#知识有限,我想有一种更简单的方法:

public void DoParse(string value, string elementname)
{
    var split = value.Split((char)39);

    XmlDocument xDoc = new XmlDocument();
    XmlElement xRoot = xDoc.CreateElement(elementname);
    xDoc.AppendChild(xRoot);

    for (var i = 0; i < split.Length - 1; i += 2)
    {
        var attribName = split[i].Replace("=", "").Trim();
        var xAttrib = xDoc.CreateAttribute(attribName);
        xAttrib.Value = split[i + 1];
        xRoot.Attributes.Append(xAttrib);
    }
    xDoc.Save(string.Format("c:\\xmlout_{0}.xml", elementname));
}

给我:

  

<Customer xmlns:z="#RowsetSchema" ows_ID="1" ows_ContentTypeId="0x0106006324F8B638865542BE98AD18210EB6F4" ows_ContentType="Contact" ows_Title="Mouse" ows_Modified="2009-08-12 14:53:50" ows_Created="2009-08-12 14:53:50" ows_Author="1073741823;#System Account" ows_Editor="1073741823;#System Account" ows_owshiddenversion="1" ows_WorkflowVersion="1" ows__UIVersion="512" ows__UIVersionString="1.0" ows_Attachments="0" ows__ModerationStatus="0" ows_LinkTitleNoMenu="Mouse" ows_LinkTitle="Mouse" ows_SelectTitle="1" ows_Order="100.000000000000" ows_GUID="{37A91B6B-B645-446A-8E8D-DA8250635DE1}" ows_FileRef="1;#Lists/customersList/1_.000" ows_FileDirRef="1;#Lists/customersList" ows_Last_x0020_Modified="1;#2009-08-12 14:53:50" ows_Created_x0020_Date="1;#2009-08-12 14:53:50" ows_FSObjType="1;#0" ows_PermMask="0x7fffffffffffffff" ows_FileLeafRef="1;#1_.000" ows_UniqueId="1;#{28A223E0-100D-49A6-99DA-7947CFC38B18}" ows_ProgId="1;#" ows_ScopeId="1;#{79BF21FE-0B9A-43B1-9077-C071B61F5588}" ows__EditMenuTableStart="1_.000" ows__EditMenuTableEnd="1" ows_LinkFilenameNoMenu="1_.000" ows_LinkFilename="1_.000" ows_ServerUrl="/Lists/customersList/1_.000" ows_EncodedAbsUrl="http://spvm:3333/Lists/customersList/1_.000" ows_BaseName="1_" ows_MetaInfo="1;#" ows__Level="1" ows__IsCurrentVersion="1" ows_FirstName="Mickey" ows_FullName="Mickey Mouse" ows_Comments="&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;" ows_ServerRedirected="0" />

任何人都有一些意见吗?感谢。