我需要加載2種類型的xml文檔;一個擁有50個子女,另一個擁有50個和800個額外的子女。對於較小的文檔,性能非常好,對於較大的文檔可以接受,直到孩子的數量增加。 20k個孩子* 50個子孩子=表現優異,20k個孩子* 850個子孩子=表現緩慢。當他們不存在時,我將如何跳過尋找額外的後代?我最初的嘗試讓我認爲我需要爲小文檔和大文檔分別提供類,方法,視圖模型和視圖。以下是對我的代碼的簡要介紹。我該如何提高加載xml文檔的速度
public class MyItem
{
private string layout;
private string column;
private string columnSpan;
private string row;
private string rowSpan;
private string background;
public MyItem(string layout, string column, string columnSpan, string row, string rowSpan, string background)
{
Layout = layout;
Column = column;
ColumnSpan = columnSpan;
Row = row;
RowSpan = rowSpan;
Background = background;
}
public string Layout
{
get { return this.layout; }
set { this.layout = value; }
}
(未顯示 - 列,ColumnSpan,行,行跨度和背景它的處理方式相同佈局)
只是在這個例子中,下面顯示只有6分的孩子,我要找只有前2個子子文件才能加載xml文檔。通過這種方式,我可以使用無論是小型還是大型XML文檔所需的任何加載方法。
internal class myDataSource
{
//Loads (MyList) xml file
public static List<MyItem> Load(string MyListFilename)
{
var myfiles = XDocument.Load(MylistFilename).Descendants("item").Select(
x => new MyItem(
(string)x.Element("layout"),
(string)x.Element("column"),
(string)x.Element("columnSpan"),
(string)x.Element("row"),
(string)x.Element("rowSpan"),
(string)x.Element("background")));
return myfiles.ToList();
}
public class MainViewModel : ViewModelBase
{
public void LoadMyList()
{
this.myfiles = new ObservableCollection<MyItemViewModel>();
List<MyItem> mybaseList = myDataSource.Load(MyListFilename);
foreach (MyItem myitem in mybaseList)
{
this.myfiles.Add(new MyItemViewModel(myitem));
}
this.mycollectionView = (ICollectionView)CollectionViewSource.GetDefaultView(myfiles);
if (this.mycollectionView == null)
throw new NullReferenceException("mycollectionView");
}
}
public class MyItemViewModel: ViewModelBase
{
private Models.MyItem myitem;
public MyItemViewModel(MyItem myitem)
{
if (myitem == null)
throw new NullReferenceException("myitem");
this.myitem = myitem;
}
public string Layout
{
get
{
return this.myitem.Layout;
}
set
{
this.myitem.Layout = value;
OnPropertyChanged("Layout");
}
}
(未顯示 - 列,ColumnSpan,行,行跨度和背景它的處理方式相同佈局)
+1,如果您不想讀取所有子節點,最好避免調用讀取每個子節點的方法... – user7116
每個子節點都有某種數據,只是不是所有的子子女都有人居住。較小的文檔就像一個大綱,只有基本的數據。更大的文檔包含所有基本數據以及可能需要的所有其他內容。小的一個是裸骨,另一個提供非常豐富的體驗,但都有相同數量的子節點。 –
我同意sixlettervariables,我添加了另一種小文檔去大文檔的方法,但是這給了我一個錯誤,說MyItem不包含一個構造,需要少量的參數。當我開始處理每個問題時,很明顯我需要兩件事 - 我試圖避免。 –