STORM STORM - 2 months ago 8
C# Question

How to get a value from this XML?

I am working with SharePoint lists and in one case trying to retrieve data from a so called MicroFeed list. This lists item has a property called

XML
which has the following content

<z:row xmlns:z='#RowsetSchema' ows_ID='10' ows_ContentTypeId='0x0100E38CAF07792E6046939F01845ADCB038' ows_ContentType='Base' ows_Title='Administrator said...' ows_Modified='2016-09-19 17:46:33' ows_Created='2016-09-19 17:46:33' 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='Administrator said...' ows_LinkTitle='Administrator said...' ows_LinkTitle2='Administrator said...' ows_SelectTitle='10' ows_Order='1000.00000000000' ows_GUID='{85B2882D-9E06-4E82-BB80-3E5E56DFB37B}' ows_FileRef='10;#anfragen/100004/Lists/PublishedFeed/FEB96200-6E92-41DB-856B-E8702BCDF33A/10_.000' ows_FileDirRef='10;#anfragen/100004/Lists/PublishedFeed/FEB96200-6E92-41DB-856B-E8702BCDF33A' ows_Last_x0020_Modified='10;#2016-09-19 17:46:33' ows_Created_x0020_Date='10;#2016-09-19 17:46:33' ows_FSObjType='10;#0' ows_SortBehavior='10;#0' ows_PermMask='0x7fffffffffffffff' ows_FileLeafRef='10;#10_.000' ows_UniqueId='10;#{B5C32F85-5C1C-4BC2-9277-794189FC890A}' ows_ProgId='10;#' ows_ScopeId='10;#{C76EE74B-5FA4-449D-A071-D1B416877394}' ows__EditMenuTableStart='10_.000' ows__EditMenuTableStart2='10' ows__EditMenuTableEnd='10' ows_LinkFilenameNoMenu='10_.000' ows_LinkFilename='10_.000' ows_LinkFilename2='10_.000' ows_ServerUrl='/anfragen/100004/Lists/PublishedFeed/FEB96200-6E92-41DB-856B-E8702BCDF33A/10_.000' ows_EncodedAbsUrl='http://sp2013/anfragen/100004/Lists/PublishedFeed/FEB96200-6E92-41DB-856B-E8702BCDF33A/10_.000' ows_BaseName='10_' ows_MetaInfo='10;#' ows__Level='1' ows__IsCurrentVersion='1' ows_ItemChildCount='10;#0' ows_FolderChildCount='10;#0' ows_MicroBlogType='2' ows_PostAuthor='LAB\Administrator' ows_DefinitionId='1' ows_RootPostID='1' ows_RootPostOwnerID='8.5d6de4b5f62a4dcf8061e9b7896b9b18.cd4152d91406452eae9b277b6f818aa0.5d6de4b5f62a4dcf8061e9b7896b9b18.0c37852b34d0418e91c62ac25af4be5b' ows_RootPostUniqueID='9928c5e5a8894800965c41e04a5e69aa' ows_ReplyCount='0' ows_Attributes='0' ows_Content='HEy this is the ntext i need' ows_ContentData='&lt;CDC xmlns:i=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://Microsoft/Office/Server/SPMicrofeedContentDataCollection&quot;&gt;&lt;a xmlns:d2p1=&quot;http://Microsoft/Office/Server/Microfeed&quot;&gt;&lt;d2p1:CD&gt;&lt;d2p1:a&gt;ContentUri&lt;/d2p1:a&gt;&lt;d2p1:b i:nil=&quot;true&quot; /&gt;&lt;d2p1:c&gt;http://sp2013/anfragen/100004/Lists/PublishedFeed/FEB96200-6E92-41DB-856B-E8702BCDF33A&lt;/d2p1:c&gt;&lt;d2p1:d i:nil=&quot;true&quot; /&gt;&lt;/d2p1:CD&gt;&lt;/a&gt;&lt;/CDC&gt;' ows_SearchContent='HEy this is the ntext i need' ows_PeopleCount='0' ows_LikedBy='' ows_HashTags='' ows_TaxCatchAll='' ows_TaxCatchAllLabel='' ows_ServerRedirected='0' />


I need to extract the `ows_Content' property value from this XML. What is the easiest way to do this? Is this a valid XML which could be processed?

Answer

Load your XML with one of these two lines:

XDocument xdoc = XDocument.Load(filePath);
XDocument xdoc = XDocument.Parse(xmlString);

Get the root element that way:

XElement rootElement = xdoc.Elements().First();

Or even better, as @KSib pointed out:

XElement rootElement = xdoc.Root;

Finally, get your attribute with this line:

var content = rootElement.Attribute("ows_Content").Value;