2016-04-07 3 views
0

私は(元々の)簡単な.xml設定エディタを自分のアプリケーション用に作成しています。これらの直列化されていないXMLノードをどのように並べ替えますか?

.xmlドキュメントから特定の要素のセット(ドロップダウンから選択可能)をロードし、ユーザーが編集してDataGridに追加できるようにします。

xmlのdataGridへの読み込みがわかりました。以前の質問から編集できました。

私の最終的なハードルは、ユーザーがDataGridの行を並べ替えることができ、新しいノードがあったときに更新する必要のある多くのコードを書くことなく、追加されます。

.xml設定ファイルを編集するための現在のGUI。

Current xml config editor GUI

の.xml設定ファイルの例:

<?xml version="1.0" encoding="utf-8"?> 
<Configurations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<FlameScanners> 
    <MenuChoice>Fireye 48PT2 </MenuChoice> 
    <Body>Fireye 48PT2 </Body> 
    <Cost>0</Cost> 
    </FlameScanners> 
    <FlameScanners> 
    <MenuChoice>Fireye 85 Series </MenuChoice> 
    <Body>Fireye 85 Series </Body> 
    <Cost>0</Cost> 
    </FlameScanners> 
    <FlameScanners> 
    <MenuChoice>Fireye 95 Series </MenuChoice> 
    <Body>Fireye 95 Series </Body> 
    <Cost>0</Cost> 
    </FlameScanners> 
    <FlameScanners> 
    <MenuChoice>-by others-</MenuChoice> 
    <Body>-by others-</Body> 
    <Cost>0</Cost> 
    </FlameScanners> 
</Configurations> 

そしてこのすべてを扱う関連するコードの抜粋:私の質問を繰り返すと

private Configurations deserializedXML; 
public ConfigEditorWindow() 
{ 
    InitializeComponent(); 
    fillSectionDropdown(); 
    this.deserializedXML = Deserialize<Configurations>(); 
} 

private void comboBox_Section_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    if (isComboBoxLoaded == false) 
     return; 
    switch (comboBox_Section.SelectedValue.ToString()) 
    { 
     case "FlameScanners": 
      dataGrid.ItemsSource = deserializedXML.FlameScanners; 
      break; 
      ... 
      ... 
      ... 
    } 
} 
private static T Deserialize<T>() where T : new() 
{ 
    // Create an instance of T 
    T ReturnListOfT = CreateInstance<T>(); 

    // Create a new file stream for reading the XML file 
    using (FileStream ReadFileStream = new FileStream(ConfigFile, FileMode.Open, FileAccess.Read, FileShare.Read)) 
    { 
     // Construct a XmlSerializer and use it 
     // to serialize the data from the stream. 
     XmlSerializer SerializerObj = new XmlSerializer(typeof(T)); 
     try 
     { 
      // Deserialize the hashtable from the file 
      ReturnListOfT = (T)SerializerObj.Deserialize(ReadFileStream); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message)); 
     } 
    } 
    // return the Deserialized data. 
    return ReturnListOfT; 
} 

[XmlRoot(ElementName = "FlameScanners")] 
public class FlameScanners 
{ 
    [XmlElement(ElementName = "MenuChoice")] 
    public string MenuChoice { get; set; } 
    [XmlElement(ElementName = "Body")] 
    public string Body { get; set; } 
    [XmlElement(ElementName = "Cost")] 
    public string Cost { get; set; } 
} 
... //Repeated for each element in the config file 
... 
[XmlRoot(ElementName = "Configurations")] 
public class Configurations 
{ 

    [XmlElement(ElementName = "Scope")] 
    public List<Scope> Scope { get; set; } 

    [XmlElement(ElementName = "Company")] 
    public List<Company> Company { get; set; } 

    [XmlElement(ElementName = "ControlStrategies")] 
    public List<ControlStrategies> ControlStrategies { get; set; } 

    [XmlElement(ElementName = "CustomerSpecs")] 
    public List<CustomerSpecs> CustomerSpecs { get; set; } 

    [XmlElement(ElementName = "SystemSpecs")] 
    public List<SystemSpecs> SystemSpecs { get; set; } 

    [XmlElement(ElementName = "ControlSystem")] 
    public List<ControlSystem> ControlSystem { get; set; } 

    [XmlElement(ElementName = "CsHw")] 
    public List<CsHw> CsHw { get; set; } 

    [XmlElement(ElementName = "FlameScanners")] 
    public List<FlameScanners> FlameScanners { get; set; } 

    [XmlElement(ElementName = "Enclosures")] 
    public List<Enclosures> Enclosures { get; set; } 

    [XmlElement(ElementName = "Options")] 
    public List<Options> Options { get; set; } 

    [XmlElement(ElementName = "Documents")] 
    public List<Documents> Documents { get; set; } 

    [XmlElement(ElementName = "Notes")] 
    public List<Notes> Notes { get; set; } 
} 

。 dataGridの特定の行を並べ替える最良の方法は何でしょうか?私はデータグリッドがにリンクされているデータを変更する必要があることを知っているので、私のような何かを行うに多分考えた:

if (dataGrid.SelectedItem == null || dataGrid.SelectedIndex <= 0 || dataGrid.SelectedIndex > deserializedXML.Company.Count-1) 
       return; 
      Company c = deserializedXML.Company[dataGrid.SelectedIndex]; 
      deserializedXML.Company.RemoveAt(dataGrid.SelectedIndex); 
      deserializedXML.Company.Insert(dataGrid.SelectedIndex - 1, c); 
      dataGrid.Items.Refresh(); 

しかし、これはユーザーがどの要素に応じて、switch文にする必要がありますが現在視聴中。誰も簡単な方法を見ることができますか?

ありがとうございます!

答えて

0

データをソート、グループ化、およびフィルタリングするには、コレクションビューを使用します。あなたは以下のこの達成する方法についてのチュートリアルを見つけることができます

:私のコードを掘り、いくつかの後

How to: Group, Sort, and Filter Data in the DataGrid Control

+0

XMLを新しい順序でファイルに保存することができますか? – Sentenial

0

は、私は最善の方法は、私は設定要素を格納したかを再構築すると判断しました。

各要素(ScopeクラスやCompanyクラスなど)ごとに異なるクラスを使用する代わりに、各config要素に対応できるクラスを1つ作成しました。

このように、ConfigurationsクラスのpublicメンバーをCurrentlySelectedという名前にできました。ユーザーが別の要素を表示するたびに更新されます。 次に、アイテムを並べ替える場合。私はCurrentlySelectedメンバーに変更を加えることができます。

これにより、新しい設定要素が必要なたびにたくさんのコードを追加する必要がなくなりました。

関連する問題