LINQ を使用して、Sharepoint WSS3 のlistService.GetListItems()
メソッドから返された XmlNode をフィルター処理しています。
返される XML は次のとおりです: http://pastebin.com/mqHcserY
最後の行が他の行とは異なり、次の属性が含まれていないことがわかりました。
ows_ContentType
ows_LinkFilenameNoMenu
ows_LinkFilename
ows_BaseName
ows__EditMenuTableStart
したがって、これを念頭に置いて、LINQ で結果をフィルタリングします。
XmlNode items = listService.GetListItems(listName, string.Empty, query, viewFields, string.Empty, queryOptions, g.ToString());
// Namespace for z: prefix
XNamespace xns = "#RowsetSchema";
XElement root;
using (XmlReader xr = new XmlNodeReader(items)) { root = XElement.Load(xr); }
// This query returns XElements that are Folder types
IEnumerable<XElement> result = from child in root.Descendants(xns + "row")
where child.Attribute("ows_ContentType").Value != null && child.Attribute("ows_ContentType").Value == "Folder"
select child;
foreach (XElement xml in result)
{
//Exception when attempts to loop final XElement
}
ただし、結果を反復処理すると、NullReferenceException
. Foreach ループでは、最後のオブジェクトに到達するまですべてのオブジェクトを反復処理し、例外をスローします。
この最後のオブジェクトは、他の行とは異なるものであり (消去のプロセスを通じてこれを知っています。他のすべての行が過去にループされるのを見ました)、"ows_ContentType" 属性がないため、結果から除外する必要がありました。
where child.Attribute("ows_ContentType").Value != null && child.Attribute("ows_ContentType").Value == "Folder"
null値を含むものをすべてフィルタリングしようとしてLINQを変更しましたが、結果は同じです。いくつかのサンプルを確認しましたが、構文は正しいようです。
誰かがこの最後の XElement が null を返す原因を説明できますか? <IEnumerable<XElement> result
コレクションに null インスタンスを追加する理由が本当にわかりません。