简体   繁体   English

从REST API调用获取xml节点值

[英]Get xml node value from REST API call

We are trying to get the Url from the entries of the list below by using c#. 我们正在尝试使用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>

We are however experiencing a problem with our code. 但是,我们的代码遇到了问题。 When debugging, the url of the webRequest is correct but the itemList remains empty. 调试时,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);

Edit: 编辑:

Using the solution offered by Alex Thompson we have been able to narrow down the list of problems. 使用Alex Thompson提供的解决方案,我们可以缩小问题列表的范围。 After editing the code, I've been debugging my program and noticed that the XML below is all I get returned into my streamreader: 编辑代码后,我一直在调试程序,并注意到下面的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>

This is undoubtedly not the XML it should return. 无疑,这不是它应该返回的XML。 If someone could point me in the right direction to what the cause of this problem may be, it would be greatly appreciated.I 如果有人能将我正确地指出导致此问题的原因,那将不胜感激。

You could do something like this: 您可以执行以下操作:

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);

If you want to select multiple values into an object, you could do something like: 如果要在一个对象中选择多个值,可以执行以下操作:

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