问题
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();
which gives me the following:
<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=""7"">
<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=""7"">
<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>
How can I get the d:Title
and d:Body
for each entry
?
I tried to use:
foreach (XElement element in xd.Descendants("entry"))
{
Console.WriteLine(element);
}
but that didn't work.
Please help me get the nodes in entry
.
回答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:
You should use the XName instead of tag name.
foreach (XElement element in xd.Descendants("{http://www.w3.org/2005/Atom}entry"))
{
Console.WriteLine(element);
}
//To read the d:title and 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);
}
This is working for me.
回答3:
What you have here is not a standard xml but an atom feed. Try this: ASP.net c#. How do I parse an atom feed from a blog Or google a framework
来源:https://stackoverflow.com/questions/42839628/how-to-get-certain-elements-inside-an-xml-document