2

I am attempting to write a LINQ query to pull `ows_Alert="This is the text for an alert" into a list. there may be more than 1 "ows_Alert" so what I'm trying to do is retrieve a list of alerts, and place them as items of a split button drop-down menu.

I tried, attributes, and I tried elements, but htey are neither? How do I retrieve the ows_Alert value?

My code is as follows:

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

The XML File

<?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>
5
  • XPath, or LINQ over XPath. Commented Aug 16, 2013 at 21:24
  • stackoverflow.com/questions/11235139/… Commented Aug 16, 2013 at 21:26
  • @RobertMcKee it's LINQ to XML (msdn.microsoft.com/en-us/library/bb675156.aspx) Commented Aug 16, 2013 at 21:27
  • Any sample where there may be more than 1 "ows_Alert" Commented Aug 16, 2013 at 21:27
  • @I4V Yes, there may be several alerts or items with the attribute(?) ows_Alert Commented Aug 16, 2013 at 21:38

3 Answers 3

4
XNamespace z = "#RowsetSchema";

var alerts = xDocument.Descendants(z + "row")
                    .Select(row => (string)row.Attribute("ows_Alert"))
                    .ToList();
Sign up to request clarification or add additional context in comments.

Comments

3
   var ns = XNamespace.Get("#RowsetSchema");
   var alerts = xml
      .Descendants(ns + "row")
      .Select(row => row.Attribute("ows_Alert").Value);

Comments

2

You have to include the namespaces when traversing the document. I tried this when using your xml in "test.xml" and it worked.

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.