1

私のフォームには 2 つの WPF PropertyGrid があり、PropertyGrid ごとに異なる名前の 2 つの xml ファイルを作成する必要があります。インターネットで探しましたが、何も見つかりませんでした。2 つの xml ファイルを作成することは可能ですか?

最初の PropertyGrid の私のクラス:

public class Tube {}
public class OvalTube : Tube {}
public class Dome : Tube {}

2 番目の PropertyGrid の場合:

public class Intersection {}
public class TubeIntersection : Intersection {}
public class Macro : Intersection {}

また、PropertyGrid ごとに 1 つの xml ファイルがあります。

xml を保存するためのコード:

private Type tube = typeof(Tube);
private Type[] intersection = { typeof(Intersection)};

public bool SaveXml(Tube m_tube, Intersection m_intersecion, string m_fileName)
    {
        FileStream fs = new FileStream(m_fileName, FileMode.Create);

        XmlSerializer serializer = new XmlSerializer(this.tube, this.intersection);

        serializer.Serialize(fs, m_tube);
        fs.Close();

        return true;
    }

しかし、2 つの xml ファイルを作成できません。

そして、私がそうするとき:

public bool SaveXml(Tube m_tube, Intersection m_intersection, string m_fileName)
    {

        using (StreamWriter writer = new StreamWriter(m_fileName))
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Tube));
                serializer.Serialize(writer, m_tube);
                serializer.Serialize(writer, m_intersection);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                writer.Flush();
                writer.Close();
            }
        }
        return true;
    }

エラーコードがあります。

チューブの PropertyGrid の初期化

namespace Prototyp1_1.GUI.ViewModel
{
public class PartViewModel : ViewModelBase
{
    private Tube m_tube;
    private TubeType m_tubeType;

    public PartViewModel()
    {
        m_tubeType = TubeType.Not_Specified;
        SetTubeTypes();
    }
    public TubeType _TubeType
    {
        get { return m_tubeType; }
        set
        {
            if (value == m_tubeType)
                return;
            m_tubeType = value;                
            OnTubeTypeChanged();
            base.OnPropertyChanged("_TubeType");
        }
    }
    public List<TubeType> TubeTypes { get; set; }

    public Tube _Tube
    {
        get { return m_tube; }
        set
        {
            m_tube = value;
            base.OnPropertyChanged("_Tube");
        }
    }

    public void OnTubeTypeChanged()
    {
        switch (m_tubeType)
        {
            case TubeType.BentTube:
                _Tube = new BentTube();
                break;
            case TubeType.Dome:
                _Tube = new Dome();
                break;
            case TubeType.OvalTube:
                _Tube = new OvalTube();
                break;
            case TubeType.RectangularTube:
                _Tube = new RectangularTube();
                break;
            case TubeType.RoundTube:
                _Tube = new RoundTube();
                break;
        }
        MainViewModel.GetInstance().IntersectionToolbar._TubeType = m_tubeType;
    }
    private void SetTubeTypes()
    {
        Array values = Enum.GetValues((typeof(TubeType)));
        TubeTypes = new List<TubeType>();
        foreach (TubeType value in values)
        {
            TubeTypes.Add(value);
        }
        TubeTypes.Remove(TubeType.Not_Specified);
    }
}

}

交差点の PropertyGrid:

namespace Prototyp1_1.GUI.ViewModel
{
public class IntersectionViewModel : ViewModelBase
{
    Intersection _intersection;
    IntersectionRepository _intersectionRepository;
    ObservableCollection<CommandViewModel> _intersections;

    ICollectionView _intersectionView;
    bool _isSelected;

    public IntersectionViewModel()
    {
        _intersectionRepository = new IntersectionRepository();
        _intersectionView = CollectionViewSource.GetDefaultView(_intersectionRepository._Intersections);
        _IntersectionView.CurrentChanged += IntersectionSelectionChanged;
    }

    public IntersectionViewModel(Intersection intersection, IntersectionRepository intersectionRepository)
    {
        if (intersection == null)
            throw new ArgumentNullException("intersection");
        if (intersectionRepository == null)
            throw new ArgumentNullException("intersectionRepository");
        _intersection = intersection;
        _intersectionRepository = intersectionRepository;
    }
    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            if (value == _isSelected)
                return;
            _isSelected = value;
            base.OnPropertyChanged("IsSelected");
        }
    }

    public Intersection _Intersection
    {
        get { return _intersection; }
        set
        {
            if (value == null)
                return;
            _intersection = value;
            OnPropertyChanged("_Intersection");
        }
    }

    public IntersectionType _IntersectionType
    {
        get { return _intersection._IntersectionType; }
        set
        {
            if (value == _intersection._IntersectionType)
                return;
            _intersection._IntersectionType = value;
            base.OnPropertyChanged("Intersection");
        }
    }

    public IntersectionRepository _IntersectionRepository
    {
        get { return _intersectionRepository; }
    }

    public ICollectionView _IntersectionView
    {
        get { return _intersectionView; }
    }
    public ObservableCollection<CommandViewModel> _Intersections
    {
        set
        {
            _intersections = value;
            base.OnPropertyChanged("Commands");
        }
        get
        {
            if (_intersections == null)
            {
                _intersections = new ObservableCollection<CommandViewModel>();
            }
            return _intersections;
        }
    }
    public void addIntersectionListItem(IntersectionListItem intersection)
    {
        _Intersection = intersection._Intersection;
        _IntersectionRepository.AddIntersection(intersection);
        _IntersectionView.Refresh(); // update the view
    }

    public void onIntersectionClicked()
    {
        object o = _IntersectionView.CurrentItem;
    }
    void IntersectionSelectionChanged(object sender, EventArgs e)
    {
        IntersectionListItem i = (IntersectionListItem)_IntersectionView.CurrentItem;

        if(i != null)
            _Intersection = i._Intersection;
    }
}

}

PartViewModelll および IntersectionModelllView クラスの初期化とメソッドの呼び出し:

public class MainViewModel : ViewModelBase
{
    IntersectionToolbarViewModel m_intersectionToolbar;
    PartViewModel m_partView;
    string _projectFolder;
    private static MainViewModel instance = null;

    public MainViewModel()
    {
        base.DisplayName = Strings.MainViewModel_DisplayName;
        _projectFolder = "";
    }

    public static MainViewModel GetInstance()
    {
        if (instance == null)
        {
            instance = new MainViewModel();
        }
        return instance;
    }

    List<CommandViewModel> CreateCommands()
    {
        return new List<CommandViewModel>
        {                
            new CommandViewModel(
                LocalizedStrings.Instance.getLocalizedCommands("MainViewModel_Command_Save"),
                new ActionCommand(param => this.Save())),
        };
    }
    public PartViewModel PartView
    {
        get
        {
            if (m_partView == null)
            {
                m_partView = new PartViewModel();
            }
            return m_partView;
        }
    }
    public IntersectionViewModel IntersectionView
    {
        get
        {
            if (m_intersectionView == null)
            {
                m_intersectionView = new IntersectionViewModel();
            }
            return m_intersectionView;
        }
    }

    void Save()
    {
        PartSerializer part = new PartSerializer();
        Microsoft.Win32.SaveFileDialog saveDialog = new Microsoft.Win32.SaveFileDialog();
        saveDialog.InitialDirectory = @"C:\";
        saveDialog.Filter = "Text documents (.xml)|*.xml";
        if (saveDialog.ShowDialog() == true)
        {
            _projectFolder = saveDialog.FileName;
            part.SaveXml(PartView._Tube, IntersectionView._Intersection, saveDialog.FileName);
        }
    }
}
4

2 に答える 2

0

XML要素のタイプを指定してみましたか?

[XmlElement("Intersection", Type=typeof(Intersection))]
[XmlElement("TubeIntersection", Type = typeof(TubeIntersection))] 
public Intersection TubeOrNoTubeIntersection { get; set; }

-

シリアル化しようとしている構造と発生しているエラーを投稿すると、より良い答えが得られます

于 2012-11-29T15:35:25.540 に答える
0

XmlSerializerforTubeは、タイプのプロパティをシリアル化できません。次のことIntersectionを試してください。

using (StreamWriter writer = new StreamWriter(m_fileName))
{       
    XmlSerializer serializer = new XmlSerializer(typeof(Tube));
    serializer.Serialize(writer, m_tube);
    // Exception handling omitted for brevity
}

using (StreamWriter writer = new StreamWriter(m_fileName2))
{
    XmlSerializer serializer2 = new XmlSerializer(typeof(Intersection ));
    serializer2.Serialize(writer, m_intersection);
    // Exception handling omitted for brevity
}

2番目のファイル名varを導入しましたm_fileName2が、もちろんこれを処理できます。


より一般的にするには、両方に単純な関数を使用します。

function ToXmlFile(object o, string filename)
{
    using (StreamWriter writer = new StreamWriter(filename))
    {
        var serializer = new Xml.Serialization.XmlSerializer(o.GetType();
        serializer.Serialize(writer, o);
        // Exception handling omitted for brevity
    }
}
于 2012-11-29T15:26:17.877 に答える