从 SHAREPOINT REST API 结果中读取 XML 值
read the XML Value from the SHAREPOINT RESTAPI result
请问如何在 foreach 循环或 for 循环中从 SHAREPOINT RESTAPI 结果中读取 XML 值?
这是我的源代码,
我想读取 d:Title,d:Description,and d:Follow/d:Url
的值
XmlNamespaceManager xmlnspm = new XmlNamespaceManager(new NameTable());
Uri sharepointUrl = new Uri("SHAREPOINT URL);
xmlnspm.AddNamespace("atom", "http://www.w3.org/2005/Atom");
xmlnspm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
xmlnspm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
NetworkCredential cred = new System.Net.NetworkCredential("USERNAME", "PASSWORD", "DOMAIN");
HttpWebRequest listRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "_api/lists/getByTitle('" + "LIST NAME" + "')/items");
listRequest.Method = "GET";
listRequest.Accept = "application/atom+xml";
listRequest.ContentType = "application/atom+xml;type=entry";
listRequest.Credentials = cred;
HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse();
StreamReader listReader = new StreamReader(listResponse.GetResponseStream());
XmlDocument listXml = new XmlDocument();
listXml.LoadXml(listReader.ReadToEnd());
和
我的示例 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xml:base="https://mvponduty.sharepoint.com.com/sites/sg/daw/_api/">
<id>ce656942-44db-472b-93cd-0caeb82f8ffb</id>
<title />
<updated>2019-12-03T01:52:11Z</updated>
<entry m:etag=""8"">
<content type="application/xml">
<m:properties>
<d:Title>User guide - Bloomberg for advanced users</d:Title>
<d:Follow m:type="SP.FieldUrlValue">
<d:Description>Follow</d:Description>
<d:Url>https://mvponduty.sharepoint.com/sites/sg/daw/_layouts/SubNew.aspx?List=%7B3EE60A7B%2D88C4%2D44E4%2D81AC%2DFEC9D91764F5%7D&ID=113&Source=http%3A%2F%2Fportal%2Easpac%2Ekworld%2Ekpmg%2Ecom%2Fsg%2Fdw%2FPages%2Fdefault%2Easpx</d:Url>
</d:Follow>
</m:properties>
</content>
</entry>
<entry m:etag=""8"">
<content type="application/xml">
<m:properties>
<d:Title>User guide - Bloomberg for advanced users</d:Title>
<d:Follow m:type="SP.FieldUrlValue">
<d:Description>Follow</d:Description>
<d:Url>https://mvponduty.sharepoint.com/sites/sg/daw/_layouts/SubNew.aspx?List=%7B3EE60A7B%2D88C4%2D44E4%2D81AC%2DFEC9D91764F5%7D&ID=113&Source=http%3A%2F%2Fportal%2Easpac%2Ekworld%2Ekpmg%2Ecom%2Fsg%2Fdw%2FPages%2Fdefault%2Easpx</d:Url>
</d:Follow>
</m:properties>
</content>
</entry>
</feed>
尝试了几种方法后,
我找到了解决方案。
这里有我的示例源代码。
// Get and display all the title and Description
XmlElement root = listXml.DocumentElement;
XmlNodeList elemList = root.GetElementsByTagName("content");
XmlNodeList elemList_title = root.GetElementsByTagName("d:Title");
XmlNodeList elemList_desc = root.GetElementsByTagName("d:Description");
for (int i = 0; i < elemList.Count; i++)
{
string title = elemList_title[i].InnerText;
string description = elemList_desc[i].InnerText;
Console.WriteLine(title+description);
}
我建议您使用 JSON 访问 REST,我们可以更轻松地获得结果。
示例代码:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace REST
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("Site URL/_api/web/lists/getByTitle(List Name')/items");
endpointRequest.Method = "GET";
endpointRequest.Accept = "application/json;odata=verbose";
NetworkCredential cred = new System.Net.NetworkCredential("username", "password", "domain");
endpointRequest.Credentials = cred;
HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
try
{
WebResponse webResponse = endpointRequest.GetResponse();
Stream webStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);
string response = responseReader.ReadToEnd();
JObject jobj = JObject.Parse(response);
JArray jarr = (JArray)jobj["d"]["results"];
foreach (JObject j in jarr)
{
Console.WriteLine(j["Title"]+" "+j["Body"]);
}
responseReader.Close();
Console.ReadLine();
}
catch (Exception e)
{
Console.Out.WriteLine(e.Message); Console.ReadLine();
}
}
}
}
请问如何在 foreach 循环或 for 循环中从 SHAREPOINT RESTAPI 结果中读取 XML 值?
这是我的源代码, 我想读取 d:Title,d:Description,and d:Follow/d:Url
的值XmlNamespaceManager xmlnspm = new XmlNamespaceManager(new NameTable());
Uri sharepointUrl = new Uri("SHAREPOINT URL);
xmlnspm.AddNamespace("atom", "http://www.w3.org/2005/Atom");
xmlnspm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
xmlnspm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
NetworkCredential cred = new System.Net.NetworkCredential("USERNAME", "PASSWORD", "DOMAIN");
HttpWebRequest listRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "_api/lists/getByTitle('" + "LIST NAME" + "')/items");
listRequest.Method = "GET";
listRequest.Accept = "application/atom+xml";
listRequest.ContentType = "application/atom+xml;type=entry";
listRequest.Credentials = cred;
HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse();
StreamReader listReader = new StreamReader(listResponse.GetResponseStream());
XmlDocument listXml = new XmlDocument();
listXml.LoadXml(listReader.ReadToEnd());
和 我的示例 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xml:base="https://mvponduty.sharepoint.com.com/sites/sg/daw/_api/">
<id>ce656942-44db-472b-93cd-0caeb82f8ffb</id>
<title />
<updated>2019-12-03T01:52:11Z</updated>
<entry m:etag=""8"">
<content type="application/xml">
<m:properties>
<d:Title>User guide - Bloomberg for advanced users</d:Title>
<d:Follow m:type="SP.FieldUrlValue">
<d:Description>Follow</d:Description>
<d:Url>https://mvponduty.sharepoint.com/sites/sg/daw/_layouts/SubNew.aspx?List=%7B3EE60A7B%2D88C4%2D44E4%2D81AC%2DFEC9D91764F5%7D&ID=113&Source=http%3A%2F%2Fportal%2Easpac%2Ekworld%2Ekpmg%2Ecom%2Fsg%2Fdw%2FPages%2Fdefault%2Easpx</d:Url>
</d:Follow>
</m:properties>
</content>
</entry>
<entry m:etag=""8"">
<content type="application/xml">
<m:properties>
<d:Title>User guide - Bloomberg for advanced users</d:Title>
<d:Follow m:type="SP.FieldUrlValue">
<d:Description>Follow</d:Description>
<d:Url>https://mvponduty.sharepoint.com/sites/sg/daw/_layouts/SubNew.aspx?List=%7B3EE60A7B%2D88C4%2D44E4%2D81AC%2DFEC9D91764F5%7D&ID=113&Source=http%3A%2F%2Fportal%2Easpac%2Ekworld%2Ekpmg%2Ecom%2Fsg%2Fdw%2FPages%2Fdefault%2Easpx</d:Url>
</d:Follow>
</m:properties>
</content>
</entry>
</feed>
尝试了几种方法后, 我找到了解决方案。
这里有我的示例源代码。
// Get and display all the title and Description
XmlElement root = listXml.DocumentElement;
XmlNodeList elemList = root.GetElementsByTagName("content");
XmlNodeList elemList_title = root.GetElementsByTagName("d:Title");
XmlNodeList elemList_desc = root.GetElementsByTagName("d:Description");
for (int i = 0; i < elemList.Count; i++)
{
string title = elemList_title[i].InnerText;
string description = elemList_desc[i].InnerText;
Console.WriteLine(title+description);
}
我建议您使用 JSON 访问 REST,我们可以更轻松地获得结果。
示例代码:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace REST
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("Site URL/_api/web/lists/getByTitle(List Name')/items");
endpointRequest.Method = "GET";
endpointRequest.Accept = "application/json;odata=verbose";
NetworkCredential cred = new System.Net.NetworkCredential("username", "password", "domain");
endpointRequest.Credentials = cred;
HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
try
{
WebResponse webResponse = endpointRequest.GetResponse();
Stream webStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);
string response = responseReader.ReadToEnd();
JObject jobj = JObject.Parse(response);
JArray jarr = (JArray)jobj["d"]["results"];
foreach (JObject j in jarr)
{
Console.WriteLine(j["Title"]+" "+j["Body"]);
}
responseReader.Close();
Console.ReadLine();
}
catch (Exception e)
{
Console.Out.WriteLine(e.Message); Console.ReadLine();
}
}
}
}