首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从SHAREPOINT结果读取XML值

问从SHAREPOINT结果读取XML值
EN

Stack Overflow用户
提问于 2019-12-03 02:28:38
回答 2查看 557关注 0票数 0

我是否可以询问如何在foreach循环或for循环中从SHAREPOINT结果读取XML值?

这里是我的源代码,我想阅读d:、d:Description和d:W循/d:Url的值

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
<?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="&quot;8&quot;">
      <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&amp;ID=113&amp;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="&quot;8&quot;">
      <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&amp;ID=113&amp;Source=http%3A%2F%2Fportal%2Easpac%2Ekworld%2Ekpmg%2Ecom%2Fsg%2Fdw%2FPages%2Fdefault%2Easpx</d:Url>
            </d:Follow>
         </m:properties>
      </content>
   </entry>
</feed>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-03 08:06:50

我建议您使用JSON访问REST,我们可以更容易地获得结果。

示例代码

代码语言:javascript
复制
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();  
            }       
        }  
    }  
} 
票数 1
EN

Stack Overflow用户

发布于 2019-12-03 08:02:18

在尝试了几种方法之后,我找到了解决办法。

这里有我的示例源代码。

代码语言:javascript
复制
    // 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);

                }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59149377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档