如何在XML文档中获取某些元素

时间:2017-03-16 16:25:45

标签: c# xml linq-to-xml sharepoint-2013

我正在查询我的SP列表:

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>

如何为每个d:Title获取d:Bodyentry

我试图使用:

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

但这没效果。

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

3 个答案:

答案 0 :(得分:2)

您应该使用XName而不是标记名称。

 foreach (XElement element in xd.Descendants("{http://www.w3.org/2005/Atom}entry"))
            {
              Console.WriteLine(element);
            }

//阅读d:title和d:body,

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");
            foreach (XElement element in xd.Descendants("{http://www.w3.org/2005/Atom}entry"))
            {
                //Iterate the child element [ element.Descendants(title)], to get the title and body
                //To simply the code, I used FirstOrDefault here
                Console.WriteLine(element.Descendants(title).FirstOrDefault().Value);
                Console.WriteLine(element.Descendants(body).FirstOrDefault().Value);               

            }

这对我有用。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

这里有的不是标准的xml,而是原子提要。 试试这个:ASP.net c#. How do I parse an atom feed from a blog 或谷歌一个框架