2016-09-19 68 views
1

我正在使用SharePoint列表並嘗試從所謂的MicroFeed列表中檢索數據。這裏列出的項目有一個名爲XML屬性,它具有以下內容如何從此XML獲取值?

<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' /> 

我需要提取此XML的`ows_Content」屬性值。什麼是最簡單的方法來做到這一點?這是一個可以處理的有效XML嗎?

+0

查看['System.Xml.Linq'](https://msdn.microsoft.com/en-us/library/system.xml.linq(v = vs.110).aspx) – juharr

+0

使用適當的XML解析器,如System.Xml.Linq中的解析器。乍一看,該文本看起來像有效的XML。 –

回答

4

裝入XML與這兩個行之一:

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

獲得根元素的方法:

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

甚至更​​好,因爲@KSib指出:

XElement rootElement = xdoc.Root; 

最後,通過以下行獲得屬性:

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

'xdoc.Root'也應該可以工作 – KSib

+0

@KSib:可以給個示例嗎? – STORM

+3

查看他在哪裏使用'xdoc.Elements()。First()'?我在說'xdoc.Root'也可以。我正在擴大他已經正確的答案。 – KSib