开发者

LINQ to XML problem with SharePoint Web Services

I am using SharePoint Web Services to get some list items out of SharePoint for a project i am working on.

I am using using LINQ to XML开发者_运维百科 to parse the resulting XML to be put into a datasheet. The problem i am running into is when trying to parse an item that isn't required in SharePoint...

var fields = from item in results.Descendants(XName.Get("row", "#RowsetSchema"))
                     select new
                     {
                         ID = item.Attribute("ows_ID").Value,
                         Title = item.Attribute("ows_Title").Value,
                         DNS = item.Attribute("ows_DNS_x0020_Name").Value
                     };

DNS Name is not a required item in the list and some items do not have an entry for this. the resulting xml from sharepoint omits the field from the XML causing a "Object reference not set to an instance of an object." exception.

is there a workaround for this without me having to put a where clause in the LINQ statement (just because there isn't a DNS Name entered does not mean that i don't want it to show up in the results)


var fields = from item in results.Descendants(XName.Get("row", "#RowsetSchema")) 
             select new 
             { 
                ID = item.Attribute("ows_ID").Value, 
                Title = item.Attribute("ows_Title").Value, 
                DNS = item.Attribute("ows_DNS_x0020_Name") == null ? "" : item.Attribute.("ows_DNS_x0020_Name").Value 
             }; 

Wouldn't that work?


See my answer regarding adding a ViewFields parameter to your query here: Soapclient query a Sharepoint web service

You'd add a DNS_x0020_Name FieldRef to your particular ViewFields parameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜