【问题标题】:How to read the XML response from SharePoint REST api call如何从 SharePoint REST api 调用读取 XML 响应
【发布时间】:2017-09-15 08:28:26
【问题描述】:

我正在获取 REST 调用的 XML 响应,其中每个条目包含多个 子节点。基本上任何Lookup 字段或User 字段都会产生一个子节点。 谁能帮我理解如何阅读这些子节点?因为元素名称是Id 或Email,这是重复的。对于非常长的 XML 感到抱歉,但这是仅获取 2 个项目的结果。

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://ppespcollab.amat.com/sites/SSAS/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
    <id>f7f3e0d8-73aa-4bd9-a11c-c8d2c884895c</id>
    <title />
    <updated>2017-09-14T09:51:55Z</updated>
    <entry m:etag="&quot;5&quot;">
        <id>Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)</id>
        <category term="SP.Data.RequestsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Name" type="application/atom+xml;type=entry" title="Name" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)/Name">
            <m:inline>
                <entry>
                    <id>d724a6a3-c532-4f43-baaf-f3a25bbaccca</id>
                    <category term="SP.Data.UserInfoItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                    <title />
                    <updated>2017-09-14T09:51:55Z</updated>
                    <author>
                        <name />
                    </author>
                    <content type="application/xml">
                        <m:properties>
                            <d:Id m:type="Edm.Int32">31210</d:Id>
                            <d:EMail>Ishan_Halarnkar@contractor.amat.com</d:EMail>
                        </m:properties>
                    </content>
                </entry>
            </m:inline>
        </link>
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Segment" type="application/atom+xml;type=entry" title="Segment" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)/Segment">
            <m:inline>
                <entry>
                    <id>9d4306ec-4b01-4624-984b-79445b9d2ec1</id>
                    <category term="SP.Data.SegmentsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                    <title />
                    <updated>2017-09-14T09:51:55Z</updated>
                    <author>
                        <name />
                    </author>
                    <content type="application/xml">
                        <m:properties>
                            <d:Title>Test - TechM</d:Title>
                            <d:Id m:type="Edm.Int32">10</d:Id>
                        </m:properties>
                    </content>
                </entry>
            </m:inline>
        </link>
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/BU" type="application/atom+xml;type=feed" title="BU" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)/BU">
            <m:inline>
                <feed>
                    <id>7d3cf6e4-9fdc-4c21-90e9-5112b963dc49</id>
                    <title />
                    <updated>2017-09-14T09:51:55Z</updated>
                    <entry>
                        <id>44c3ff84-4650-4d85-a6dc-908639338be4</id>
                        <category term="SP.Data.Business_x0020_UnitsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                        <title />
                        <updated>2017-09-14T09:51:55Z</updated>
                        <author>
                            <name />
                        </author>
                        <content type="application/xml">
                            <m:properties>
                                <d:Title>CMP</d:Title>
                                <d:Id m:type="Edm.Int32">14</d:Id>
                            </m:properties>
                        </content>
                    </entry>
                </feed>
            </m:inline>
        </link>
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Author0" type="application/atom+xml;type=feed" title="Author0" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)/Author0">
            <m:inline>
                <feed>
                    <id>0f399745-10f4-40f0-be4e-1f9d69222d49</id>
                    <title />
                    <updated>2017-09-14T09:51:55Z</updated>
                    <entry>
                        <id>4c0509ce-62c4-4e90-9689-134b145b02a0</id>
                        <category term="SP.Data.UserInfoItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                        <title />
                        <updated>2017-09-14T09:51:55Z</updated>
                        <author>
                            <name />
                        </author>
                        <content type="application/xml">
                            <m:properties>
                                <d:Id m:type="Edm.Int32">31210</d:Id>
                                <d:EMail>Ishan_Halarnkar@contractor.amat.com</d:EMail>
                            </m:properties>
                        </content>
                    </entry>
                </feed>
            </m:inline>
        </link>
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CC_x0020_List" type="application/atom+xml;type=feed" title="CC_x0020_List" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)/CC_x0020_List" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Publication_x0020_Type" type="application/atom+xml;type=entry" title="Publication_x0020_Type" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(160)/Publication_x0020_Type">
            <m:inline>
                <entry>
                    <id>a002d855-e51b-4f4f-b8ec-3725cf4aecb9</id>
                    <category term="SP.Data.Publication_x0020_TypeListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                    <title />
                    <updated>2017-09-14T09:51:55Z</updated>
                    <author>
                        <name />
                    </author>
                    <content type="application/xml">
                        <m:properties>
                            <d:Title>Presentation</d:Title>
                            <d:Id m:type="Edm.Int32">2</d:Id>
                        </m:properties>
                    </content>
                </entry>
            </m:inline>
        </link>
        <title />
        <updated>2017-09-14T09:51:55Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:Id m:type="Edm.Int32">160</d:Id>
                <d:Title>asssfsfdsf</d:Title>
                <d:End_x0020_Use>BLR</d:End_x0020_Use>
                <d:Approval_x0020_Type>Parallel</d:Approval_x0020_Type>
                <d:Due_x0020_Date m:type="Edm.DateTime">2017-09-26T05:00:00Z</d:Due_x0020_Date>
                <d:Description>sdfssfsfssf</d:Description>
                <d:Status>Submitted</d:Status>
                <d:History>Request submitted by Ishan Halarnkar --CNTR on Wednesday, September 13, 2017 8&amp;#58;15 AM&lt;/br&gt;</d:History>
                <d:RequestID>2017_00000160</d:RequestID>
                <d:EndUseYear>2014</d:EndUseYear>
                <d:ID m:type="Edm.Int32">160</d:ID>
            </m:properties>
        </content>
    </entry>
    <entry m:etag="&quot;7&quot;">
        <id>Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)</id>
        <category term="SP.Data.RequestsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Name" type="application/atom+xml;type=entry" title="Name" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)/Name">
            <m:inline>
                <entry>
                    <id>b76cc22b-f985-444a-873a-46ae2cd0a10a</id>
                    <category term="SP.Data.UserInfoItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                    <title />
                    <updated>2017-09-14T09:51:55Z</updated>
                    <author>
                        <name />
                    </author>
                    <content type="application/xml">
                        <m:properties>
                            <d:Id m:type="Edm.Int32">6760</d:Id>
                            <d:EMail>Denise_Schmidt@amat.com</d:EMail>
                        </m:properties>
                    </content>
                </entry>
            </m:inline>
        </link>
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Segment" type="application/atom+xml;type=entry" title="Segment" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)/Segment">
            <m:inline>
                <entry>
                    <id>6e70b80b-fff6-4662-8f53-26fc977f34ef</id>
                    <category term="SP.Data.SegmentsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                    <title />
                    <updated>2017-09-14T09:51:55Z</updated>
                    <author>
                        <name />
                    </author>
                    <content type="application/xml">
                        <m:properties>
                            <d:Title>Testing Segment 0917</d:Title>
                            <d:Id m:type="Edm.Int32">11</d:Id>
                        </m:properties>
                    </content>
                </entry>
            </m:inline>
        </link>
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/BU" type="application/atom+xml;type=feed" title="BU" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)/BU">
            <m:inline>
                <feed>
                    <id>f5e5e76d-07b7-442f-b6bf-73d9ed65e708</id>
                    <title />
                    <updated>2017-09-14T09:51:55Z</updated>
                    <entry>
                        <id>07bd9eed-23f7-4b10-8f38-42e0845f19ca</id>
                        <category term="SP.Data.Business_x0020_UnitsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                        <title />
                        <updated>2017-09-14T09:51:55Z</updated>
                        <author>
                            <name />
                        </author>
                        <content type="application/xml">
                            <m:properties>
                                <d:Title>ds Testing</d:Title>
                                <d:Id m:type="Edm.Int32">24</d:Id>
                            </m:properties>
                        </content>
                    </entry>
                </feed>
            </m:inline>
        </link>
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Author0" type="application/atom+xml;type=feed" title="Author0" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)/Author0">
            <m:inline>
                <feed>
                    <id>717cf9a2-9f0a-4265-bb32-1c90989ba6ed</id>
                    <title />
                    <updated>2017-09-14T09:51:55Z</updated>
                    <entry>
                        <id>4c593a4e-5c2e-4f1a-8c99-cb2d81b33af3</id>
                        <category term="SP.Data.UserInfoItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                        <title />
                        <updated>2017-09-14T09:51:55Z</updated>
                        <author>
                            <name />
                        </author>
                        <content type="application/xml">
                            <m:properties>
                                <d:Id m:type="Edm.Int32">6760</d:Id>
                                <d:EMail>Denise_Schmidt@amat.com</d:EMail>
                            </m:properties>
                        </content>
                    </entry>
                </feed>
            </m:inline>
        </link>
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CC_x0020_List" type="application/atom+xml;type=feed" title="CC_x0020_List" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)/CC_x0020_List" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Publication_x0020_Type" type="application/atom+xml;type=entry" title="Publication_x0020_Type" href="Web/Lists(guid'16b26978-b5d8-407a-a82d-e66017158895')/Items(159)/Publication_x0020_Type">
            <m:inline>
                <entry>
                    <id>62f94289-1e4b-4e83-94fe-1f518bde639b</id>
                    <category term="SP.Data.Publication_x0020_TypeListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                    <title />
                    <updated>2017-09-14T09:51:55Z</updated>
                    <author>
                        <name />
                    </author>
                    <content type="application/xml">
                        <m:properties>
                            <d:Title>Presentation</d:Title>
                            <d:Id m:type="Edm.Int32">2</d:Id>
                        </m:properties>
                    </content>
                </entry>
            </m:inline>
        </link>
        <title />
        <updated>2017-09-14T09:51:55Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:Id m:type="Edm.Int32">159</d:Id>
                <d:Title>Test on 9/12/17</d:Title>
                <d:End_x0020_Use>TPAS</d:End_x0020_Use>
                <d:Approval_x0020_Type>Parallel</d:Approval_x0020_Type>
                <d:Due_x0020_Date m:type="Edm.DateTime">2017-09-28T05:00:00Z</d:Due_x0020_Date>
                <d:Description>testing email notices</d:Description>
                <d:Status>Submitted</d:Status>
                <d:History>Request delegated by Denise Schmidt (ds Testing - CTO) on Tuesday, September 12, 2017 2&amp;#58;16 PM&lt;/br&gt;Request approved by Denise Schmidt (ds Testing - Manager) on Tuesday, September 12, 2017 2&amp;#58;15 PM&lt;/br&gt;Request submitted by Denise Schmidt on Tuesday, September 12, 2017 2&amp;#58;12 PM&lt;/br&gt;</d:History>
                <d:RequestID>2017_00000159</d:RequestID>
                <d:EndUseYear m:null="true" />
                <d:ID m:type="Edm.Int32">159</d:ID>
            </m:properties>
        </content>
    </entry>

我使用的代码是但没有给出预期的结果:

XmlDocument listXml = new XmlDocument();
            XmlNamespaceManager xmlnspm = new XmlNamespaceManager(new NameTable());
            xmlnspm.AddNamespace("atom", "http://www.w3.org/2005/Atom");
            xmlnspm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
            xmlnspm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
            listXml.LoadXml(response);
            var prop = listXml.SelectNodes("//atom:entry/atom:content/m:properties", xmlnspm);
            List<string> sdsd = new List<string>();
            foreach (XmlNode ndlist in prop)

            {

                sdsd.Add(ndlist.SelectSingleNode("d:Id", xmlnspm).InnerXml +
                    ndlist.SelectSingleNode("d:EMail", xmlnspm).InnerXml +
                     ndlist.SelectSingleNode("d:Status", xmlnspm).InnerXml+
                     ndlist.SelectSingleNode("d:History", xmlnspm).InnerXml);
            }

我正在寻找的结果是: 对于每个条目,请阅读

  1. 如果姓名则姓名(ID 和电子邮件)
  2. If Segment then Segment(Title and Id)
  3. 如果是 BU,那么 BU(标题和 ID)
  4. 如果 Author0 则 Author0(Id 和电子邮件) ...同样适用于所有子节点 对于所有主要节点,我需要该值
  5. 身份证
  6. 标题
  7. End_x0020_Use
  8. Approval_x0020_Type

有没有办法以更易读的格式序列化这些数据,或者我必须循环每个节点? 请提出最佳选择,因为我在 XML 数据方面没有太多经验。

【问题讨论】:

  • 如果 XML 结构非常僵化并且不会改变,那么可以将其反序列化为强类型类。

标签: c# xml rest sharepoint httpresponse


【解决方案1】:

使用 xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication4
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static List<XElement> nodes;
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement feed = doc.Root;
            XNamespace ns = feed.GetDefaultNamespace();
            foreach (XElement entry in feed.Elements(ns + "entry"))
            {
                Entry newEntry = new Entry();
                Entry.entries.Add(newEntry);

                newEntry.title = (string)entry.Element(ns + "title");
                newEntry.end_x0020_Use = (string)entry.Descendants().Where(x => x.Name.LocalName == "End_x0020_Use").FirstOrDefault();
                newEntry.approval_x0020_Type = (string)entry.Descendants().Where(x => x.Name.LocalName == "Approval_x0020_Type").FirstOrDefault();

                newEntry.links = entry.Elements(ns + "link").Select(x => x.Descendants().Where(y => y.Name.LocalName == "entry").Select(y => new Link() {
                   name = (string)y.Descendants().Where(z => z.Name.LocalName == "name").FirstOrDefault(),
                   id = (string)y.Descendants().Where(z => z.Name.LocalName == "id").FirstOrDefault(),
                   email = (string)y.Descendants().Where(z => z.Name.LocalName == "EMail").FirstOrDefault(),
                   title = (string)x.Attributes().Where(z => z.Name.LocalName == "title").FirstOrDefault()
                }).FirstOrDefault()
                ).ToList();
            }

        }

    }
    public class Entry
    {
        public static List<Entry> entries = new List<Entry>();

        public string title { get; set;}
        public string end_x0020_Use { get; set; }
        public string approval_x0020_Type { get; set; }
        public List<Link> links { get; set; }

    }
    public class Link
    {
        public string name { get; set; }
        public string id { get; set; }
        public string title { get; set; }
        public string email { get; set; }
    }

}

【讨论】:

    【解决方案2】:

    我建议您在 REST API 调用中使用$filter,这样您就可以得到一个非常容易使用的 JSON 结果。 XML 检索有时可能会失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      相关资源
      最近更新 更多