gpt4 book ai didi

c# - 使用 Web 服务遍历 Sharepoint 列表项的属性,得到 "Object reference not set to an instance of an object"

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:50 25 4
gpt4 key购买 nike

我正在使用 C# 调用 Sharepoint 网络服务。我想获得一个 Sharepoint 列表,然后针对列表中的每个项目,将其属性写入控制台。当我使用 foreach 遍历属性时,我不断收到“未设置对象实例的对象引用”。代码如下:

class Program
{
static void Main(string[] args)
{
try
{
cSharpTest_service.Lists lists = new cSharpTest_service.Lists();
lists.Url = "http://wsssandbox/sites/cSharp/_vti_bin/lists.asmx";
lists.Credentials = System.Net.CredentialCache.DefaultCredentials;
System.Xml.XmlNode listData = lists.GetListItems("cSharpTestList", "", null, null, "", null, "");



foreach (System.Xml.XmlNode iterateNode in listData)
{

Console.WriteLine("--NODE--");
System.Xml.XmlAttributeCollection attrs = iterateNode.Attributes;
foreach (System.Xml.XmlAttribute attr in attrs)
{
Console.WriteLine("Name:");
Console.WriteLine(attr.Name);
Console.WriteLine("Value:");
Console.WriteLine(attr.Value);
}

}
Console.ReadLine();

}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}


}
}

我认为它抛出异常是因为某些列表项的某些属性为 Null。但是我不知道如何在 foreach 迭代中检查属性是否为 null。

最佳答案

看看这个例子和这个 article .

public void getListData()
{
WS_Lists.Lists myservice = new WS_Lists.Lists();
myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
myservice.Url = "http://merdev-moss:5050/testsara/_vti_bin/Lists.asmx";

try
{
/* Assign values to pass the GetListItems method*/
string listName = "{5C65CB1A-2E1B-488A-AC07-B115CD0FC647}";
string viewName = "{75E689B4-5773-43CB-8324-58E42E1EB885}";
string rowLimit = "100";

// Instantiate an XmlDocument object
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");

/*Use CAML query*/
query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" +
"<Value Type=\"Counter\">0</Value></Gt></Where>";
viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
queryOptions.InnerXml = "";

System.Xml.XmlNode nodes = myservice.GetListItems(listName, viewName, query, viewFields, rowLimit, null, null);

foreach (System.Xml.XmlNode node in nodes)
{
if (node.Name == "rs:data")
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].Name == "z:row")
{
Response.Write(node.ChildNodes[i].Attributes["ows_Title"].Value + "</br>");
}
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

关于c# - 使用 Web 服务遍历 Sharepoint 列表项的属性,得到 "Object reference not set to an instance of an object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10693246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com