如何在XML文档中获取某些元素 [英] How to get certain elements inside an XML document

查看:109
本文介绍了如何在XML文档中获取某些元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这样查询我的SP列表:

I am querying my SP list like this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://myspsite/_api/web/lists/getByTitle('the%20title')/items");

request.Method = "GET";
request.ContentType = "text/xml";
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

XDocument xd = XDocument.Load(readStream);
response.Close();
readStream.Close();

这给了我以下内容:

<feed xml:base="http://myspsite/_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>090ds-211312-asdadsasd</id>
  <title />
  <updated>2011-03-16T15:33:25Z</updated>
  <entry m:etag="&quot;7&quot;">
    <id>a4846531-dae6-413c-bbbe-b3012342ewerred0d1</id>
    <category term="SP.Data.My.ListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <link rel="edit" href="Web/Lists(guid'0eef-a924-471e-80f0-8d71erwsdf3d6d')/Items(10)" />
    <title />
    <updated>2012-03-16T15:33:25Z</updated>
    <author>
      <name />
    </author>
    <content type="application/xml">
      <m:properties>
        <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
        <d:Id m:type="Edm.Int32">38</d:Id>
        <d:ContentTypeId>0x010400C64826486CBEE74BBFF0werwerewsdfD</d:ContentTypeId>
        <d:Title>This is my Title</d:Title>
        <d:Body>This is the body</d:Body>
        <d:Summaries>This is the body summary</d:Summaries>
        <d:Include m:type="Edm.Boolean">false</d:Include>
        <d:Add_Date m:type="Edm.DateTime">2015-10-07T04:00:00Z</d:Add_Date>
        <d:ID m:type="Edm.Int32">38</d:ID>
        <d:OData__UIVersionString>1.0</d:OData__UIVersionString>
        <d:GUID m:type="Edm.Guid">69504c8e-6897-49e6-926d-d54c900524e5</d:GUID>
      </m:properties>
    </content>
  </entry>
  <entry m:etag="&quot;7&quot;">
    <id>a4846531-dae6-413c-bbbe-b301234234ewd0d1</id>
    <category term="SP.Data.My.ListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <link rel="edit" href="Web/Lists(guid'0ee1d7ef-a924-471e-80f0-8d71erwsdf3d6d')/Items(38)" />
    <title />
    <updated>2012-03-16T15:33:25Z</updated>
    <author>
      <name />
    </author>
    <content type="application/xml">
      <m:properties>
        <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
        <d:Id m:type="Edm.Int32">38</d:Id>
        <d:ContentTypeId>0x010400C64826486CBEE7sdfsfdsdf3D</d:ContentTypeId>
        <d:Title>This is my Title</d:Title>
        <d:Body>This is the body</d:Body>
        <d:Summaries>This is the body summary</d:Summaries>
        <d:Include m:type="Edm.Boolean">false</d:Include>
        <d:Add_Date m:type="Edm.DateTime">2015-10-07T04:00:00Z</d:Add_Date>
        <d:ID m:type="Edm.Int32">38</d:ID>
        <d:OData__UIVersionString>1.0</d:OData__UIVersionString>
        <d:GUID m:type="Edm.Guid">69504c8e-6897-49e6-926d-d54c900524e5</d:GUID>
      </m:properties>
    </content>
  </entry>
  //... more entry
</feed>

如何获取每个entryd:Titled:Body?

我尝试使用:

foreach (XElement element in xd.Descendants("entry"))
{
  Console.WriteLine(element);
}

但这没用.

请帮助我获取entry中的节点.

Please help me get the nodes in entry.

推荐答案

    XName properties = XName.Get("properties", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
    XName title = XName.Get("Title", "http://schemas.microsoft.com/ado/2007/08/dataservices");
    XName body = XName.Get("Body", "http://schemas.microsoft.com/ado/2007/08/dataservices");
    var result = xd.Descendants(properties).Select(en => new { title = en.Element(title), body = en.Element(body) });

这篇关于如何在XML文档中获取某些元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆