2

我正在尝试编写一个 LINQ 查询以将 `ows_Alert="This is the text for an alert" 拉入列表。可能有超过 1 个“ows_Alert”,所以我要做的是检索警报列表,并将它们作为拆分按钮下拉菜单的项目。

我试过了,属性,我试过元素,但都不是?如何检索 ows_Alert 值?

我的代码如下:

pAlerts= xDocument.Elements().ToList();
pAlerts.ForEach(item => tsSplitBtnAlerts.DropDownItems
          .Add(item.Attribute("ows_Alert").ToString()));

XML 文件

<?xml version="1.0" encoding="utf-8" ?>
<listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" xmlns="http://schemas.microsoft.com/sharepoint/soap/">
  <rs:data ItemCount="1">
    <z:row ows_ContentTypeId="0x0100D2769100E1771B4A94C86F01916A3C4F" 
           ows_Title="Alert Test" 
           ows_Alert="This is the text for an alert" 
           ows_ID="1" ows_ContentType="Item" 
           ows_Modified="2013-08-02 11:19:07" ows_Created="2013-08-02 11:19:07"  ows_Author="1;#REDACTED" ows_Editor="1;#REDACTED"
           ows_owshiddenversion="1"  ows_WorkflowVersion="1" ows__UIVersion="512" ows__UIVersionString="1.0" 
           ows_Attachments="0" ows__ModerationStatus="0" ows_LinkTitleNoMenu="Alert Test" ows_LinkTitle="Alert Test" 
           ows_LinkTitle2="Alert Test" ows_SelectTitle="1" ows_Order="100.000000000000" ows_GUID="{77BD9162-461F-4A97-89E3-033E387E76A9}" 
           ows_FileRef="1;#Lists/PortalToolbarAlerts/1_.000" ows_FileDirRef="1;#Lists/PortalToolbarAlerts" 
           ows_Last_x0020_Modified="1;#2013-08-02 11:19:07" ows_Created_x0020_Date="1;#2013-08-02 11:19:07" ows_FSObjType="1;#0" 
           ows_SortBehavior="1;#0" ows_PermMask="0xb008431061" ows_FileLeafRef="1;#1_.000" ows_UniqueId="1;#{3E3EA8F8-16B2-4DD0-81B1-BAA9592302E9}" 
           ows_ProgId="1;#" ows_ScopeId="1;#{4310D927-E486-4B8C-8034-52937AC5A6D8}" ows__EditMenuTableStart="1_.000" ows__EditMenuTableStart2="1"
           ows__EditMenuTableEnd="1" ows_LinkFilenameNoMenu="1_.000" ows_LinkFilename="1_.000" ows_LinkFilename2="1_.000"
           ows_ServerUrl="/Lists/PortalToolbarAlerts/1_.000" ows_EncodedAbsUrl="http://REDACTEDSITE/1_.000" 
           ows_BaseName="1_" ows_MetaInfo="1;#" ows__Level="1" ows__IsCurrentVersion="1" ows_ItemChildCount="1;#0" ows_FolderChildCount="1;#0" />
  </rs:data>
</listitems>
4

3 回答 3

4
XNamespace z = "#RowsetSchema";

var alerts = xDocument.Descendants(z + "row")
                    .Select(row => (string)row.Attribute("ows_Alert"))
                    .ToList();
于 2013-08-16T21:32:44.337 回答
3
   var ns = XNamespace.Get("#RowsetSchema");
   var alerts = xml
      .Descendants(ns + "row")
      .Select(row => row.Attribute("ows_Alert").Value);
于 2013-08-16T21:28:30.590 回答
2

遍历文档时必须包含命名空间。我在“test.xml”中使用你的 xml 时尝试了这个,它起作用了。

XDocument document = XDocument.Load(Server.MapPath("~/test.xml"), LoadOptions.None);
XNamespace ns = "urn:schemas-microsoft-com:rowset";
XNamespace z = "#RowsetSchema";
IEnumerable<XElement> datas = document.Root.Elements(ns + "data");
foreach (XElement data in datas)
{
    IEnumerable<XElement> rows = data.Elements(z + "row");
    string alerts = "";
    foreach (XElement row in rows)
    {
        alerts += row.Attribute("ows_Alert").Value + "<br />";
    }
    Test.Text = alerts;
}
于 2013-08-16T21:46:41.960 回答