我的表单中有两个WPF PropertyGrid,需要为每个PropertyGrid创建两个不同名称的xml文件。我在互联网上寻找但没有找到任何东西。是否可以创建两个xml文件?
我的第一个PropertyGrid类:
public class Tube {}
public class OvalTube : Tube {}
public class Dome : Tube {}
第二个PropertyGrid:
public class Intersection {}
public class TubeIntersection : Intersection {}
public class Macro : Intersection {}
并为每个PropertyGrid提供一个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;
}
但我无法创建两个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;
}
我有错误代码。
初始化TubeGrid for Tube
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;
}
}
}
初始化PartViewModell和IntersectionModellView类并调用方法:
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);
}
}
}
答案 0 :(得分:0)
XmlSerializer
的{{1}}无法序列化Tube
类型的媒体资源,请尝试:
Intersection
请注意,我们引入了第二个文件名var 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
}
,但您可以处理此问题,但是您需要这样做。
为了使它更通用,可以对两者使用简单的函数:
m_fileName2
答案 1 :(得分:0)
您是否尝试过指定XML元素的类型?
[XmlElement("Intersection", Type=typeof(Intersection))]
[XmlElement("TubeIntersection", Type = typeof(TubeIntersection))]
public Intersection TubeOrNoTubeIntersection { get; set; }
-
如果你发布了你想要序列化的结构以及你得到的错误,你会得到更好的答案