我以为我在这里的其他帖子中解决了这个问题,所以请不要将其标记为重复 - 事实证明它仍然只是在列表中添加了一个项目而且它是最后一项。这不是将项目添加到列表的正确方法吗?由于某种原因,它只会将最后一项添加到列表中:
public List<ViewPresentation> GetColumnsFromXml(string ColumnsXmlString, string xpath)
{
XDocument doc = XDocument.Parse(ColumnsXmlString);
XNamespace ns = XNamespace.Get("http://schemas.microsoft.com/SystemCenter/Common/UI/Views/GridView");
var expressions = (from ds in doc.Root.Descendants(ns + "Column") select ds).ToList();
List<ViewPresentation> listColumns = new List<ViewPresentation>();
ViewPresentation VP = new ViewPresentation();
for (int i = 0; i < expressions.Count; i++)
{
var xml = XElement.Parse(expressions[i].ToString());
VP.columName = xml.Attribute("Name").Value;
VP.displayName = xml.Attribute("DisplayName").Value;
VP.memberBinding = xml.Attribute("DisplayMemberBinding").Value;
VP.property = xml.Attribute("Property").Value;
VP.dataType = xml.Attribute("DataType").Value;
VP.viewwidth = xml.Attribute("Width").Value;
listColumns.Add(VP);
}
return listColumns;
}
这是我试图获取值的xml:
<Columns>
<mux:ColumnCollection xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:mux="http://schemas.microsoft.com/SystemCenter/Common/UI/Views/GridView" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:datebinding="clr-namespace:Microsoft.EnterpriseManagement.UI.Extensions;assembly=Microsoft.EnterpriseManagement.UI.Extensions" xmlns:data="clr-namespace:Microsoft.EnterpriseManagement.UI.Extensions;assembly=Microsoft.EnterpriseManagement.UI.Extensions" xmlns:appCommon="clr-namespace:Microsoft.EnterpriseManagement.ServiceManager.Application.Common;assembly=Microsoft.EnterpriseManagement.ServiceManager.Application.Common" xmlns:toolbox="clr-namespace:Microsoft.EnterpriseManagement.UI.WpfToolbox;assembly=Microsoft.EnterpriseManagement.UI.FormsInfra" xmlns:slaBinding="clr-namespace:Microsoft.EnterpriseManagement.ServiceManager.SLA.Common;assembly=Microsoft.EnterpriseManagement.ServiceManager.SLA.Common">
<mux:Column Name="ActualCost" DisplayMemberBinding="{Binding Path=ActualCost, Mode=OneWay}" Width="100" DisplayName="ActualCost.c8e7234bdd574f8097fd3b0d7fb35851" Property="ActualCost" DataType="s:Double" />
<mux:Column Name="ActualDowntimeEndDate" DisplayMemberBinding="{datebinding:DateBinding Path=ActualDowntimeEndDate, Mode=OneWay}" Width="100" DisplayName="ActualDowntimeEndDate.ed1a5af04f814341a88766f2fa18c447" Property="ActualDowntimeEndDate" DataType="s:DateTime" />
<mux:Column Name="ActualDowntimeStartDate" DisplayMemberBinding="{datebinding:DateBinding Path=ActualDowntimeStartDate, Mode=OneWay}" Width="100" DisplayName="ActualDowntimeStartDate.34daa34691ed435597c3a230ff5d8c60" Property="ActualDowntimeStartDate" DataType="s:DateTime" />
<mux:Column Name="ActualWork" DisplayMemberBinding="{Binding Path=ActualWork, Mode=OneWay}" Width="100" DisplayName="ActualWork.4afb1225100040ef8cf93871389b3380" Property="ActualWork" DataType="s:Double" />
<mux:Column Name="Description" DisplayMemberBinding="{Binding Path=Description, Mode=OneWay}" Width="100" DisplayName="Description.f461792cf5d64b10aae47e4c980792e4" Property="Description" DataType="s:String" />
<mux:Column Name="DisplayName" DisplayMemberBinding="{Binding Path=DisplayName, Mode=OneWay}" Width="100" DisplayName="DisplayName.ef4712b1ecbf4f4d9dcea38daa28269e" Property="DisplayName" DataType="s:String" />
<mux:Column Name="$DisplayName$" DisplayMemberBinding="{Binding Path=$DisplayName$, Mode=OneWay}" Width="100" DisplayName="DisplayName.99931bcb16c142cb944c3c2e43467548" Property="$DisplayName$" DataType="s:String" />
<mux:Column Name="$FullName$" DisplayMemberBinding="{Binding Path=$FullName$, Mode=OneWay}" Width="100" DisplayName="FullName.a1b287d94ab04324857568c878defda9" Property="$FullName$" DataType="s:String" />
<mux:Column Name="Impact.DisplayName" DisplayMemberBinding="{Binding Path=Impact.DisplayName, Mode=OneWay}" Width="100" DisplayName="Impact.f412c72e91104455b1695ef1ccca4e91" Property="Impact.DisplayName" DataType="s:String" />
</mux:ColumnCollection>
答案 0 :(得分:0)
移动此
ViewPresentation VP = new ViewPresentation();
循环内部。
当您想要为循环的每次迭代创建一个对象时,您正在创建单个对象。
答案 1 :(得分:0)
您正在使用相同的对象。所以你一次又一次地添加同一个对象。 添加此行
ViewPresentation VP = new ViewPresentation();
作为循环中的第一行。
for (int i = 0; i < expressions.Count; i++)
{
ViewPresentation VP = new ViewPresentation(); // this is the new line. Will create a new object for every iteration
var xml = XElement.Parse(expressions[i].ToString());
VP.columName = xml.Attribute("Name").Value;
VP.displayName = xml.Attribute("DisplayName").Value;
VP.memberBinding = xml.Attribute("DisplayMemberBinding").Value;
VP.property = xml.Attribute("Property").Value;
VP.dataType = xml.Attribute("DataType").Value;
VP.viewwidth = xml.Attribute("Width").Value;
listColumns.Add(VP);
}