Title: Is it possible to databind to an object in memory and also allow for databinding to XML
Tags: c#;wpf;xml;binding
Question: I have created an object called Project that has different properties (strings and some custom objects), I have bound text fields to these properties to get user input. I have created a method that outputs this object to an XML file. However when I import this XML file back into memory the text fields do not become populated with the text or list views of some custom objects that inherit from ObservableCollection do not have any text. The XML does load properly since if I enter text into the empty fields it updates the property and I can export an XML file with the new values.

To load the xml I use the following code

```public void LoadXML()
    {
        OpenFileDialog fileDialog = new OpenFileDialog();
        fileDialog.Title = "Load XML File";
        fileDialog.Filter = "XML Files|*.xml";
        DialogResult result = fileDialog.ShowDialog();

        if (result.ToString().Equals("OK"))
        {
            string filePath = fileDialog.FileName.ToString();
            XmlSerializer serializer = new XmlSerializer(typeof(Project));
            TextReader textReader = new StreamReader(filePath);
            newProject = (Project)serializer.Deserialize(textReader);
            textReader.Close();
        }

    }
```

Any suggestions would be welcomed, thanks.

Comment: Does the Project class implement the INotifyPropertyChanged interface and does it call throw the PropertyChanged event for every property changed? Are you using WPF?

Here is the accepted answer: I assume you use WPF.

You need to implement the INotfiyPropertyChanged-Interface and throw its event for every Property of your class that is tied to a Control.

WPF then updates your GUI accordingly, when you deserialize the Project from XML. If it does not, check if the DataContext of your control is set to the Project instance that you deserialized.

Comment for this answer: Had INotifyPropertyChanged implemented, it was the DataContext of the control that wasn't set. Didn't realise it had to be set after de-serialisation. Thanks for the help.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:10/30/10', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><wpf><xml><binding>', 'Title': 'Is it possible to databind to an object in memory and also allow for databinding to XML', 'ViewCount': 114, 'id': 4059014, 'user_AboutMe': '', 'user_AccountId': 186248.0, 'user_DisplayName': 'Ciaran', 'user_Reputation': 23.0, 'user_Views': 4.0, 'user_id': 423500.0}}