我正在使用 C# 调用 Sharepoint Web 服务。我想得到一个 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
但是我不知道如何在迭代过程中检查属性是否为空。