如何阅读包含Sharepoint List Structure的XElement内容?

时间:2013-04-24 10:00:06

标签: c# xml linq xml-parsing

我想使用c#将shareche List的Choice列中的MultipleCheckbox项绑定到asp.net CheckBoxListItem。 我正在使用XELEMENT重新获取List的信息:

在.cs文件中:

XElement listStructure;

listStructure = proxy.GetList("WebsiteSubscriber");

这里得到XML:

  <List DocTemplateUrl="" DefaultViewUrl="/Lists/WebsiteSubscriber/AllItems.aspx" MobileDefaultViewUrl="" ID="{2C8A80EA-38C5-48F7-9D7D-400D445A5E64}" Title="WebsiteSubscriber" Description="" ImageUrl="/_layouts/images/itgen.png" Name="{2C8A80EA-38C5-48F7-9D7D-400D445A5E64}" BaseType="0" FeatureId="00bfea71-de22-43b2-a848-c05709900100" ServerTemplate="100" Created="20130417 02:18:11" Modified="20130424 09:27:11" LastDeleted="20130419 04:46:25" Version="5" Direction="none" ThumbnailSize="" WebImageWidth="" WebImageHeight="" Flags="545263616" ItemCount="13" AnonymousPermMask="0" RootFolder="/Lists/WebsiteSubscriber" ReadSecurity="1" WriteSecurity="1" Author="8" EventSinkAssembly="" EventSinkClass="" EventSinkData="" EmailAlias="" WebFullUrl="/" WebId="198e057a-38e8-410a-8358-ed95f77d18ea" SendToLocation="" ScopeId="e2cbf2fd-93e4-408d-bd4e-320321734b8c" MajorVersionLimit="0" MajorWithMinorVersionsLimit="0" WorkFlowId="" HasUniqueScopes="False" NoThrottleListOperations="False" HasRelatedLists="" AllowDeletion="True" AllowMultiResponses="False" EnableAttachments="True" EnableModeration="False" EnableVersioning="False" HasExternalDataSource="False" Hidden="False" MultipleDataList="False" Ordered="False" ShowUser="True" EnablePeopleSelector="False" EnableResourceSelector="False" EnableMinorVersion="False" RequireCheckout="False" ThrottleListOperations="False" ExcludeFromOfflineClient="False" EnableFolderCreation="False" IrmEnabled="False" IsApplicationList="False" PreserveEmptyValues="False" StrictTypeCoercion="False" EnforceDataValidation="False" MaxItemsPerThrottledOperation="100000" xmlns="http://schemas.microsoft.com/sharepoint/soap/">
  <Fields>
    <Field ID="{03e45e84-1992-4d42-9116-26f756012634}" RowOrdinal="0" Type="ContentTypeId" Sealed="TRUE" ReadOnly="TRUE" Hidden="TRUE" DisplayName="Content Type ID" Name="ContentTypeId" DisplaceOnUpgrade="TRUE" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="ContentTypeId" ColName="tp_ContentTypeId" FromBaseType="TRUE" />
    <Field ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="Title" Group="Base Columns" Type="Text" DisplayName="Title" Required="FALSE" FromBaseType="TRUE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" Version="1" ColName="nvarchar1" RowOrdinal="0" />
    <Field ID="{34ad21eb-75bd-4544-8c73-0e08330291fe}" ReadOnly="TRUE" Type="Note" Name="_ModerationComments" DisplayName="Approver Comments" Hidden="TRUE" CanToggleHidden="TRUE" Filterable="FALSE" Sortable="FALSE" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="_ModerationComments" FromBaseType="TRUE" ColName="ntext1" />
    <Field ID="{bc91a437-52e7-49e1-8c4e-4698904b2b6d}" ReadOnly="TRUE" Type="Computed" Name="LinkTitleNoMenu" DisplayName="Title" Dir="" DisplayNameSrcField="Title" AuthoringInfo="(linked to item)" EnableLookup="TRUE" ListItemMenuAllowed="Prohibited" LinkToItemAllowed="Prohibited" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="LinkTitleNoMenu" FromBaseType="TRUE">
      <FieldRefs>
        <FieldRef Name="Title" />
        <FieldRef Name="LinkFilenameNoMenu" />
      </FieldRefs>
      <DisplayPattern>
        <IfEqual>
          <Expr1>
            <LookupColumn Name="FSObjType" />
          </Expr1>
          <Expr2>1</Expr2>
          <Then>
            <Field Name="LinkFilenameNoMenu" />
          </Then>
          <Else>
            <HTML><![CDATA[<a onfocus="OnLink(this)" href="]]></HTML>
            <URL />
            <HTML><![CDATA[" onclick="EditLink2(this,]]></HTML>
            <Counter Type="View" />
            <HTML><![CDATA[);return false;" target="_self">]]></HTML>
            <Column HTMLEncode="TRUE" Name="Title" Default="(no title)" />
            <IfEqual>
              <Expr1>
                <GetVar Name="ShowAccessibleIcon" />
              </Expr1>
              <Expr2>1</Expr2>
              <Then>
                <HTML><![CDATA[<img src="/_layouts/images/blank.gif" class="ms-hidden" border="0" width="1" height="1" alt="Use SHIFT+ENTER to open the menu (new window)."/>]]></HTML>
              </Then>
            </IfEqual>
            <HTML><![CDATA[</a>]]></HTML>
            <IfNew>
              <HTML><![CDATA[<img src="/_layouts/1033/images/new.gif" alt="]]></HTML>
              <HTML>New</HTML>
              <HTML><![CDATA[" class="ms-newgif" />]]></HTML>
            </IfNew>
          </Else>
        </IfEqual>
      </DisplayPattern>
    </Field>
    <Field ID="{82642ec8-ef9b-478f-acf9-31f7d45fbc31}" ReadOnly="TRUE" Type="Computed" Name="LinkTitle" DisplayName="Title" DisplayNameSrcField="Title" ClassInfo="Menu" AuthoringInfo="(linked to item with edit menu)" ListItemMenuAllowed="Required" LinkToItemAllowed="Prohibited" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="LinkTitle" FromBaseType="TRUE">
      <FieldRefs>
        <FieldRef Name="Title" />
        <FieldRef Name="LinkTitleNoMenu" />
        <FieldRef Name="_EditMenuTableStart2" />
        <FieldRef Name="_EditMenuTableEnd" />
      </FieldRefs>
      <DisplayPattern>
        <FieldSwitch>
          <Expr>
            <GetVar Name="FreeForm" />
          </Expr>
          <Case Value="TRUE">
            <Field Name="LinkTitleNoMenu" />
          </Case>
          <Default>
            <Switch>
              <Expr>
                <GetVar Name="MasterVersion" />
              </Expr>
              <Case Value="4">
                <HTML><![CDATA[<div class="ms-vb itx" onmouseover="OnItem(this)" CTXName="ctx]]></HTML>
                <Field Name="_EditMenuTableStart2" />
                <HTML><![CDATA[">]]></HTML>
                <Field Name="LinkTitleNoMenu" />
                <HTML><![CDATA[</div>]]></HTML>
                <HTML><![CDATA[<div class="s4-ctx" onmouseover="OnChildItem(this.parentNode); return false;">]]></HTML>
                <HTML><![CDATA[<span>&nbsp;</span>]]></HTML>
                <HTML><![CDATA[<a onfocus="OnChildItem(this.parentNode.parentNode); return false;" onclick="PopMenuFromChevron(event); return false;" href="javascript:;" title="Open Menu"></a>]]></HTML>
                <HTML><![CDATA[<span>&nbsp;</span>]]></HTML>
                <HTML><![CDATA[</div>]]></HTML>
              </Case>
              <Default>
                <HTML><![CDATA[<table height="100%" cellspacing="0" class="ms-unselectedtitle itx" onmouseover="OnItem(this)" CTXName="ctx]]></HTML>
                <Field Name="_EditMenuTableStart2" />
                <HTML><![CDATA["><tr><td width="100%" class="ms-vb">]]></HTML>
                <SetVar Name="ShowAccessibleIcon" Value="1" />
                <Field Name="LinkTitleNoMenu" />
                <SetVar Name="ShowAccessibleIcon" Value="0" />
                <HTML><![CDATA[</td><td><img src="/_layouts/images/blank.gif" width="13" style="visibility:hidden" alt=""/></td></tr></table>]]></HTML>
              </Default>
            </Switch>
          </Default>
        </FieldSwitch>
      </DisplayPattern>
    </Field>
    <Field ID="{5f190d91-3dbc-4489-9878-3c092caf35b6}" Hidden="TRUE" ReadOnly="TRUE" Type="Computed" Name="LinkTitle2" DisplayName="Title" DisplayNameSrcField="Title" ClassInfo="Menu" AuthoringInfo="(linked to item with edit menu) (old)" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="LinkTitle2" FromBaseType="TRUE">
      <FieldRefs>
        <FieldRef Name="Title" />
        <FieldRef Name="LinkTitleNoMenu" />
        <FieldRef Name="_EditMenuTableStart" />
        <FieldRef Name="_EditMenuTableEnd" />
      </FieldRefs>
      <DisplayPattern>
        <FieldSwitch>
          <Expr>
            <GetVar Name="FreeForm" />
          </Expr>
          <Case Value="TRUE">
            <Field Name="LinkTitleNoMenu" />
          </Case>
          <Default>
            <Field Name="_EditMenuTableStart" />
            <SetVar Name="ShowAccessibleIcon" Value="1" />
            <Field Name="LinkTitleNoMenu" />
            <SetVar Name="ShowAccessibleIcon" Value="0" />
            <Field Name="_EditMenuTableEnd" />
          </Default>
        </FieldSwitch>
      </DisplayPattern>
    </Field>
    <Field ID="{39360f11-34cf-4356-9945-25c44e68dade}" ReadOnly="TRUE" Hidden="TRUE" Type="Text" Name="File_x0020_Type" DisplaceOnUpgrade="TRUE" DisplayName="File Type" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="File_x0020_Type" FromBaseType="TRUE" ColName="nvarchar2" />
    <Field Type="Text" DisplayName="Email" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" ID="{bf605e59-6807-47de-87ac-617b2c8df00b}" SourceID="{2c8a80ea-38c5-48f7-9d7d-400d445a5e64}" StaticName="Email" Name="Email" ColName="nvarchar3" RowOrdinal="0" />
    <Field Type="MultiChoice" DisplayName="Area" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" FillInChoice="FALSE" ID="{16cc1615-a490-44de-a870-c7ebe603e2cc}" SourceID="{2c8a80ea-38c5-48f7-9d7d-400d445a5e64}" StaticName="Area" Name="Area" ColName="ntext2" RowOrdinal="0">
      <Default>Articles</Default>
      **<CHOICES>
        <CHOICE>Articles</CHOICE>
        <CHOICE>Websites</CHOICE>
        <CHOICE>Books</CHOICE>
      </CHOICES>**
    </Field>
    <Field ID="{1d22ea11-1e32-424e-89ab-9fedbadb6ce1}" ColName="tp_ID" RowOrdinal="0" ReadOnly="TRUE" Type="Counter" Name="ID" PrimaryKey="TRUE" DisplayName="ID" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="ID" FromBaseType="TRUE" />
    <Field ID="{c042a256-787d-4a6f-8a8a-cf6ab767f12d}" Type="Computed" DisplayName="Content Type" Name="ContentType" DisplaceOnUpgrade="TRUE" RenderXMLUsingPattern="TRUE" Sortable="FALSE" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="ContentType" Group="_Hidden" PITarget="MicrosoftWindowsSharePointServices" PIAttribute="ContentTypeID" FromBaseType="TRUE">

我希望从XML中获得以上值。

<CHOICES>
            <CHOICE>Articles</CHOICE>
            <CHOICE>Websites</CHOICE>
            <CHOICE>Books</CHOICE>
  </CHOICES>

1 个答案:

答案 0 :(得分:1)

如果您修复了XML,这可以实现您的目的:

var choices = (from n in xml.Descendants()
              where n.Name.LocalName == "CHOICES"
              select new
              {
                    CHOICES = n.Elements().Select(x => x.Value).ToList()
              }).ToList();

将为您提供包含CHOICES元素值的匿名类型CHOICE元素的列表。示例输出:

enter image description here

修改

见评论:

var choices = (from n in xml.Descendants()
               where n.Name.LocalName == "CHOICE"
               select n.Value).ToList();

这将返回以下字符串值的列表:

  • 文章
  • 网站
  • 图书
  • 0; #Approved
  • 1; #Rejected
  • 2; #Pending
  • 3; #Draft
  • 4; #Scheduled