2012-11-28 40 views
1

我需要加載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,行,行跨度和背景它的處理方式相同佈局)

回答

0

我認爲有一兩件事你可以做的不是做選擇一個toList和保持它爲懶惰,並返回一個Iterable,或任何選擇返回(對不起,我現在沒有一個窗口框來測試它)。當你這樣做時,你只會迭代它一次(而不是現在的兩次)

1

而不是使用Descendants,你可以按照直接路徑(即使用Elements)?這是你掃描節點,你知道沒有物品的唯一方法。

+0

+1,如果您不想讀取所有子節點,最好避免調用讀取每個子節點的方法... – user7116

+0

每個子節點都有某種數據,只是不是所有的子子女都有人居住。較小的文檔就像一個大綱,只有基本的數據。更大的文檔包含所有基本數據以及可能需要的所有其他內容。小的一個是裸骨,另一個提供非常豐富的體驗,但都有相同數量的子節點。 –

+0

我同意sixlettervariables,我添加了另一種小文檔去大文檔的方法,但是這給了我一個錯誤,說MyItem不包含一個構造,需要少量的參數。當我開始處理每個問題時,很明顯我需要兩件事 - 我試圖避免。 –

0

XDocument非常方便,但如果問題只是文件很大而且只需要掃描一次,那麼XmlReader可能是更好的選擇。它不讀取整個文件,它一次讀取一個節點。您可以手動跳過您不感興趣的部分。

+0

就我而言,我需要加載整個文檔,以便可以按關鍵字,人員,場合等進行過濾。 –