正在将 Sharepoint Web 服务 XML 列表加载到 WPF DataGrid
Loading Sharepoint Web Services XML List into WPF DataGrid
我正在尝试使用从 Sharepoint Web 服务返回的列表填充我的 DataGrid,我发现所有 tutorials/guides 都使用 XAML 文件来执行此操作。
这是我用来提取 XML 的代码,
private String GetListNode()
{
webService.Lists siteClient = new webService.Lists();
System.Net.NetworkCredential passCredentials = new System.Net.NetworkCredential("username", "password", "domain");
siteClient.Credentials = passCredentials;
XmlNode getNode = siteClient.GetListItems("Tasks", string.Empty, null, null, string.Empty, null, null);
return getNode.OuterXml;
}
这个 returns 页面 XML,我想在这个页面中获取这些项目,
ows_ID, ows_Client, ows_AssignedTo, ows_LinkTitle, ows_Status, ows_Priority, ows_DueDate
我想为 DataGrid 创建这些项目的列,然后填充数据。我们将不胜感激,在此先感谢您。
这不是完整的 XML,这是一小段,
<listitems
xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:rs="urn:schemas-microsoft-com:rowset"
xmlns:z="#RowsetSchema"
xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<rs:data ItemCount="691">
<z:row ows_Attachments="0" ows_ID="2108" ows_Task_x0020_Type="Issue" ows_Client="City University of New York" >
/>
</rs:data>
您可以考虑以下方法:
/// <summary>
/// Bind SP Data Source
/// </summary>
private void BindSPDataSource()
{
var data = GetListItems("Tasks");
var result = XElement.Parse(data.OuterXml);
XNamespace z = "#RowsetSchema";
var taskItems = from r in result.Descendants(z + "row") select new
{
TaskName = r.Attribute("ows_LinkTitle").Value,
DueDate = r.Attribute("ows_DueDate") != null ? r.Attribute("ows_DueDate").Value : string.Empty,
AssignedTo = r.Attribute("ows_AssignedTo") != null ? r.Attribute("ows_AssignedTo").Value : string.Empty,
};
dgTasks.ItemsSource = taskItems;
}
哪里
private XmlNode GetListItems(string listTitle)
{
var client = new Lists.Lists();
client.Url = webUri + "/_vti_bin/Lists.asmx";
return client.GetListItems(listTitle, string.Empty, null, null, string.Empty, null, null);
}
XAML
<DataGrid Name="dgTasks" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Task Name" Binding="{Binding TaskName}" />
<DataGridTextColumn Header="Due Date" Binding="{Binding DueDate}"/>
<DataGridTextColumn Header="Assigned To" Binding="{Binding AssignedTo}"/>
</DataGrid.Columns>
</DataGrid>
结果
我正在尝试使用从 Sharepoint Web 服务返回的列表填充我的 DataGrid,我发现所有 tutorials/guides 都使用 XAML 文件来执行此操作。
这是我用来提取 XML 的代码,
private String GetListNode()
{
webService.Lists siteClient = new webService.Lists();
System.Net.NetworkCredential passCredentials = new System.Net.NetworkCredential("username", "password", "domain");
siteClient.Credentials = passCredentials;
XmlNode getNode = siteClient.GetListItems("Tasks", string.Empty, null, null, string.Empty, null, null);
return getNode.OuterXml;
}
这个 returns 页面 XML,我想在这个页面中获取这些项目,
ows_ID, ows_Client, ows_AssignedTo, ows_LinkTitle, ows_Status, ows_Priority, ows_DueDate
我想为 DataGrid 创建这些项目的列,然后填充数据。我们将不胜感激,在此先感谢您。
这不是完整的 XML,这是一小段,
<listitems
xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:rs="urn:schemas-microsoft-com:rowset"
xmlns:z="#RowsetSchema"
xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<rs:data ItemCount="691">
<z:row ows_Attachments="0" ows_ID="2108" ows_Task_x0020_Type="Issue" ows_Client="City University of New York" >
/>
</rs:data>
您可以考虑以下方法:
/// <summary>
/// Bind SP Data Source
/// </summary>
private void BindSPDataSource()
{
var data = GetListItems("Tasks");
var result = XElement.Parse(data.OuterXml);
XNamespace z = "#RowsetSchema";
var taskItems = from r in result.Descendants(z + "row") select new
{
TaskName = r.Attribute("ows_LinkTitle").Value,
DueDate = r.Attribute("ows_DueDate") != null ? r.Attribute("ows_DueDate").Value : string.Empty,
AssignedTo = r.Attribute("ows_AssignedTo") != null ? r.Attribute("ows_AssignedTo").Value : string.Empty,
};
dgTasks.ItemsSource = taskItems;
}
哪里
private XmlNode GetListItems(string listTitle)
{
var client = new Lists.Lists();
client.Url = webUri + "/_vti_bin/Lists.asmx";
return client.GetListItems(listTitle, string.Empty, null, null, string.Empty, null, null);
}
XAML
<DataGrid Name="dgTasks" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Task Name" Binding="{Binding TaskName}" />
<DataGridTextColumn Header="Due Date" Binding="{Binding DueDate}"/>
<DataGridTextColumn Header="Assigned To" Binding="{Binding AssignedTo}"/>
</DataGrid.Columns>
</DataGrid>
结果