簡體   English   中英

從REST API調用獲取xml節點值

[英]Get xml node value from REST API call

我們正在嘗試使用c#從下面的列表條目中獲取Url。

<?xml version="1.0" encoding="utf-8" ?>
<feed xml:base="https://thomasmorestudent17.sharepoint.com/sites/devtest/_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>b82076e4-3e36-4b09-bbed-3d14e0bf948f</id> 
    <title /> 
    <updated>2014-03-19T10:21:14Z</updated> 
- <entry>
    <id>https://thomasmorestudent17.sharepoint.com/sites/devtest/_api/Web/Lists(guid'ab8811c5-0d39-457c-8fd1-c15a45c78f89')/files('Aanleiding en achtergrond van het project.docx')</id> 
    <category term="MS.FileServices.File" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
    <link rel="edit" href="Web/Lists(guid'ab8811c5-0d39-457c-8fd1-c15a45c78f89')/files('Aanleiding%20en%20achtergrond%20van%20het%20project.docx')" /> 
    <title /> 
    <updated>2014-03-19T10:21:14Z</updated> 
  - <author>
       <name /> 
    </author>
  - <content type="application/xml">
    - <m:properties>
      - <d:CreatedBy m:type="MS.FileServices.UserInformation">
            <d:Id>9</d:Id> 
            <d:Name>Thomas More</d:Name> 
        </d:CreatedBy>
      <d:ETag>"{ECAEE072-FEDD-4FF6-8A27-1EFF131B0064},1"</d:ETag> 
      <d:Id>Aanleiding en achtergrond van het project.docx</d:Id> 
      - <d:LastModifiedBy m:type="MS.FileServices.UserInformation">
            <d:Id>9</d:Id> 
            <d:Name>Thomas More</d:Name> 
        </d:LastModifiedBy>
        <d:Name>Aanleiding en achtergrond van het project.docx</d:Name> 
        <d:Size m:type="Edm.Int32">21616</d:Size> 
        <d:TimeCreated m:type="Edm.DateTime">2014-03-14T17:24:25Z</d:TimeCreated> 
        <d:TimeLastModified m:type="Edm.DateTime">2014-03-14T17:24:25Z</d:TimeLastModified> 
        <d:Url>/sites/devtest/Shared Documents/Aanleiding en achtergrond van het project.docx</d:Url> 
     </m:properties>
   </content>

但是,我們的代碼遇到了問題。 調試時,webRequest的URL是正確的,但itemList保持為空。

HttpWebRequest itemRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "/_api/Web/lists(guid'" + listId + "')/files");
        itemRequest.Method = "GET";
        itemRequest.Accept = "application/atom+xml";
        itemRequest.ContentType = "application/atom+xml;type=entry";
        itemRequest.Headers.Add("Authorization", "Bearer " + accessToken);
        HttpWebResponse itemResponse = (HttpWebResponse)itemRequest.GetResponse();
        StreamReader itemReader = new StreamReader(itemResponse.GetResponseStream());

        var itemXml = new XmlDocument();
        itemXml.LoadXml(itemReader.ReadToEnd());

        var itemList = itemXml.SelectNodes("//atom:entry/atom:content/m:properties/d:Url", xmlnspm);

編輯:

使用Alex Thompson提供的解決方案,我們可以縮小問題列表的范圍。 編輯代碼后,我一直在調試程序,並注意到下面的XML是返回到streamreader的全部內容:

<feed xml:base="https://thomasmorestudent17.sharepoint.com/sites/devtest/_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>273fa8b2-b789-41d0-9edf-01eb12657299</id>
  <title />
  <updated>2014-03-20T09:58:18Z</updated>
  <author>
    <name />
  </author>
</feed>

無疑,這不是它應該返回的XML。 如果有人能將我正確地指出導致此問題的原因,那將不勝感激。

您可以執行以下操作:

private static readonly XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
private static readonly XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";

...

XDocument doc = XDocument.Load(itemReader.ReadToEnd());
var itemsList = doc.Descendants(m + "properties").Descendants(d + "Url").Select(item => item.Value);

如果要在一個對象中選擇多個值,可以執行以下操作:

private static readonly XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
private static readonly XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";

...

var itemsList doc.Descendants(m + "properties").Select(
    item => new Item()
    {
        Id = item.Element(d + "Id").Value,
        Name = item.Element(d + "Name").Value,
        Url = item.Element(d + "Url").Value
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM