Sharepoint 2010 GetListView Linq XML
本文关键字:XML Linq GetListView 2010 Sharepoint | 更新日期: 2023-09-27 17:49:18
尝试处理web服务列表返回的XML。从sharepoint的asmx,我似乎不能得到从默认视图拉字段。
我尝试了以下
的变体 var viewFields = (from x in xData.Descendants(ns + "ListAndView")
where x.Element("View").Attribute("DefaultView").ToString() == "TRUE"
select x.Elements("ViewField")).ToList();
示例XML
<ListAndView xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<List DocTemplateUrl="">
<Fields>
<Field ID="{03e45e84-1992-4d42-9116-26f756012634}" DisplayName="test" Name="test"/>
</Fields>
<RegionalSettings>
<Language>1033</Language>
<Locale>1033</Locale>
<AdvanceHijri>0</AdvanceHijri>
<CalendarType>1</CalendarType>
<Time24>False</Time24>
<TimeZone>480</TimeZone>
<SortOrder>2070</SortOrder>
<Presence>False</Presence>
</RegionalSettings>
<ServerSettings>
<ServerVersion>0</ServerVersion>
<RecycleBinEnabled>True</RecycleBinEnabled>
<ServerRelativeUrl>/test</ServerRelativeUrl>
</ServerSettings>
</List>
<View Name="{2EDDCDF0-B93D-41A9-83B9-89E347ED93F9}" DefaultView="TRUE" >
<Query>
<OrderBy>
<FieldRef Name="ID" Ascending="FALSE" />
</OrderBy>
</Query>
<ViewFields>
<FieldRef Name="field1" />
<FieldRef Name="field2" />
<FieldRef Name="field3" />
</ViewFields>
</View>
</ListAndView>
linq语句应该确保它只从定义为默认视图的视图返回FieldRef,可以返回多个视图。
在Selman22的指导下,我完成了下面的操作
var view = xData.Descendants(ns + "View").Where(x => (string)x.Attribute("DefaultView") == "TRUE");
var f = (from x in view.Descendants(ns + "FieldRef")
select (string) x.Attribute("Name")).ToList();
这里ToString
将返回Attribute
的类型名称,而不是Value
。因此该语句将始终返回false,您将一无所获。
x.Element("View").Attribute("DefaultView").ToString() == "TRUE"
你应该使用显式强制转换来获取属性的值:
(string)x.Element("View").Attribute("DefaultView") == "TRUE"
并且您正在尝试使用x.Elements("ViewField")
Select
多个元素,因此您应该使用SelectMany
来代替List<XElement>
:
xData.Descendants(ns + "ListAndView")
.Where(x => (string)x.Element("View").Attribute("DefaultView") == "TRUE")
.SelectMany(x => x.Elements("ViewField"))
.ToList();
这应该给你所有的ViewField
元素。如果您只希望属于FieldRef
元素的Name
属性中的Values
,请使用:
xData.Descendants(ns + "ListAndView")
.Where(x => (string)x.Element("View").Attribute("DefaultView") == "TRUE")
.SelectMany(x => x.Elements("ViewField"))
.SelectMany(x => x.Elements("FieldRef").Select(y => (string)y.Attribute("Name")))
.ToList();
你可以试试没有linq吗?
这就是我要做的。
假设你有以下对象:
var listItems = client.GetListItems(listGuid, null, null, viewFields,
null, null, null);
我将使用一个简单的for循环获得遍历集合的viewfields:
var listOfViewFields = new List<string>();
foreach (XmlNode node in listItems)
{
if (node.Name == "rs:data")
{
for (int f = 0; f < node.ChildNodes.Count; f++)
{
if (node.ChildNodes[f].Name == "z:row")
{
var xmlAttributeCollection = node.ChildNodes[f].Attributes;
if (xmlAttributeCollection != null)
{
listOfViewFields.Add(xmlAttributeCollection["ows_FIELD YOU WISH TO RETRIEVE"].Value);
}
}
}
}