English 中文(简体)
使用网络服务通过 Sharepoint 列表项目的属性进行迭接, 获取“ 对象引用不设置为对象实例” 。
原标题:Iterating through attributes of Sharepoint list items using web service, getting "Object reference not set to an instance of an object"

我使用 C# 来调用 Sharepoint 网络服务 。 我想获得一个 Sharepoint 列表, 然后对列表中的每个项目, 将其属性写入控制台 。 当我在 < code> 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();
            }


        }
      }

我认为这是例外, 因为有些属性是某些列表项目的 < code> null 。 但我不知道如何检查该属性作为 < code> foreach 迭代的一部分是否为无效 。

最佳回答

看看这个例子,这个"http://sarangasl.blogspot.com/2009/12/sharepoint-list-web-service.html" rel=“noreferrer” > 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);
 }
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签