0

How I serialize the data into xml , XBMC is my custom class.

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);

This is how I create the ObservableCollection:

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

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

I am just not sure how to add all the data from XBMCSList to the collection. I am also happy to try do it a different way if any one has any ideas.

Code Behind for main window:

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;
        }
    }
}  
4

1 回答 1

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>
于 2013-09-30T09:26:58.317 回答