如何从XML中获取值

本文关键字:获取 XML | 更新日期: 2023-09-27 18:07:33

我正在使用SharePoint列表,并在一个案例中试图从所谓的MicroFeed列表中检索数据。这个列表项有一个名为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吗?

如何从XML中获取值

用以下两行之一加载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;
相关文章: