将xml序列化为自定义类,然后将值添加到可观察集合中

时间:2013-09-30 09:22:22

标签: c# .net xml

我如何将数据序列化为xml,XBMC是我的自定义类。

private static XBMC LoadXbmcShows(string XMlFile)
{
    XBMC XBMCSShowsList;
    TextReader Reader = new StreamReader(XMlFile);
    XmlSerializer serializer = new XmlSerializer(typeof(XBMC));
    XBMCSShowsList = (XBMC)serializer.Deserialize(Reader);
    Reader.Close();
    return XBMCSShowsList;
 }

XBMC XBMCSList = LoadXbmcShows(_XMLFile);

这就是我创建ObservableCollection

的方法
readonly ObservableCollection<XBMC> myCollection
    = new ObservableCollection<XBMC>()

public ObservableCollection<XBMC> MyCollection
{ get { return myCollection; } }

我只是不确定如何将XBMCSList中的所有数据添加到集合中。 如果任何人有任何想法,我也很乐意以不同的方式尝试。

主窗口的代码隐藏:

namespace MySQlXML
{  
    public partial class MainWindow : Window
    {
        static string _XMLFile = @"C:\Dump\test.xml";

        List<string> ToCopylist = new List<string>();

        readonly ObservableCollection<XBMC> myCollection
            = new ObservableCollection<XBMC>();

        public ObservableCollection<XBMC> MyCollection
        {
            get { return myCollection; }
        }


        public ObservableCollection<XBMC> MyCollection
        {
            get { return myCollection; }
        } 

        XBMC XBMCSList = LoadXbmcShows(_XMLFile);
        MyCollection.Add(XBMCSList);

        public MainWindow()
        {
            InitializeComponent();

            Showlistbox.ItemsSource = XBMCSList.Show;
            //foreach (XBMCShow show in XBMCSList.Show)
            //{
            //    Showlistbox.Items.Add(show.ShowName);

            //}
        }

        private static XBMC LoadXbmcShows(string XMlFile)
        {
            XBMC XBMCSShowsList;

            TextReader Reader = new StreamReader(XMlFile);
            XmlSerializer serializer = new XmlSerializer(typeof(XBMC));
            XBMCSShowsList = (XBMC)serializer.Deserialize(Reader);
            Reader.Close();

            return XBMCSShowsList;
        }
    }
}  

1 个答案:

答案 0 :(得分:0)

您的意思是:如何将反序列化结果添加到列表中?下面是我在构造函数中反序列化和添加的示例:

private static XBMC LoadXbmcShows(string XMlFile)
{
  XBMC XBMCSShowsList;
  TextReader Reader = new StreamReader(XMlFile);
  XmlSerializer serializer = new XmlSerializer(typeof(XBMC));
  XBMCSShowsList = (XBMC)serializer.Deserialize(Reader);
  Reader.Close();
  return XBMCSShowsList;
}

readonly ObservableCollection<XBMCShow> myCollection = 
  new ObservableCollection<XBMCShow>()

public ObservableCollection<XBMCShow> MyCollection 
{ get { return myCollection; } }

public MainWindow()
{
  InitializeComponent();

  XBMC XBMCSList = LoadXbmcShows(_XMLFile);
  myCollection = new new ObservableCollection<XBMCShow>(XBMCSList.Show);
  Showlistbox.ItemsSource = myCollection;
}

在聊天中,我得到了XBMC的定义:

public partial class XBMC 
{ 
  private XBMCShow[] showField; 

  [System.Xml.Serialization.XmlElementAttribute("Show")] 
  public XBMCShow[] Show 
  { 
    get { return this.showField; } 
    set { this.showField = value; } 
  } 
}

..和XAML:

<ListBox x:Name="Showlistbox"> 
  <ListBox.ItemTemplate> 
    <DataTemplate> 
      <TextBlock Text="{Binding ShowName}" /> 
    </DataTemplate> 
  </ListBox.ItemTemplate> 
</ListBox>