WPF ObservableCollection two way binding XML file - c#

I want to bind an ObservableCollection to an XML file. Before the multiple replies of
You should bind your DataGrid, ComboBox, etc.. directly to the XML file
Please note that the ObservableCollection already exists and is already being bound to DataGrid, ComboBox etc... Re-writing all that code doesn't sound like fun at all to me. Although every search I do about binding an XML file to a ObservableCollection returns binding object directly the XML file.
Yes I know I could manually do the add, update, and delete myself but was hoping to not have to do that.

manually do the add, update, and delete myself
Please don't delete yourself. ;-)
Xml node processing is a complex process and does not easily lend itself to such a scenario as adding and deleting from a string list which is what the question is patterning itself off of....
At some point code is needed as a go between, the visual and storage, to facilitate the process of managing the xml structure as adds and deletes are done.

Microsoft Blend has support for binding straight to XML. Perhaps you could add some design time data to the form in Blend, and see what it generates. It might give you some ideas.

Related

How do I save and load data from a DataGridView?

I have been looking for a while now for a way to do this but nothing seems to be what I am looking for, but all I want is to enter in some data into a DataGridView, save it and then if I want, later load it back up. All the tutorial seem to focus on log in data from some database or something like that when all I want is a grid where I can put things in and add a search filter tool so I can look things up. It is for a project I am doing and I was wanting a way to save and organise data efficiently like how you can as if it was a text document but in a grid with a search filter as well as the way you can send file over to someone else who has the application and read the data. Sorry if I have put it in a confusing way.
I am doing this in Visual Studio 2019 in C#
You could use this one as a staring point. Although such approaches seem to be outdated. (Using datatables etc) Your best bet is using classes for serializing the data to json and sending the json file

Can I create cascaded and dependent entries in a C# project

although I have a concrete case that I want to solve this is a more generic question.
I have a text template that creates a C# file. The text template loads an xml which is also included in the project. Currently anytime I edit the xml and save it, I have to go to the text template and also save it to update my C# file.
In the project the settings are for the C# file that it is the text template, but I guess that this is only a reference to display the items correctly in the Solution Explorer. If I now edit the project file and put also a to the text template to depend upon the XML file, it get the correct structure.
The only problem is, that if I save the XML, the text template is not automatically triggered. So the question is, if there is a possibility to link the items so that I get cascaded updates, so that I only have to modify the XML and the rest is updated accordingly?
This is now my special case, but in the end the question is, if there is a possibility in general?
Thanks
Martin

C# saving and loading all form elements?

I'm trying to design my C# winform application with a very generalized function to automatically go through all of the form elements and save their states/values in a text file so that I can load it later. I want it to be very generalized so that it'll be a cinch to reuse this code in future projects, as it wouldn't be heavily tied down to the specifics.
The form elements I want to save are text boxes, combo boxes, data grid views, list boxes and that's about it. I want to save their values and everything about them.
One way that I was going about it was to go through every possible form element and then detect eachs type, and then create the corresponding c# code to re-create its value ('tboxmine.value="blue elephant"'), and then writing the code to a file, so that I could load the code from the file and execute it using the CSCcompiler. My code so far doesn't seem to be working correctly and I'm having my doubts that this compiler is actually running the code inside my application (I think it's possibly creating a new thread?), and it just seems like there's probably a far more straightforward relatively standard way of doing this.
This seems a bit like the reverse "best practice" approach. If you dont't know about databinding I suggest you look into that.
Basically you create classes to represent your data and use databinding to associate controls with your objects. The controls will automatically show the right value and allow the user to change it. If the user has changed the value, your object gets automatically updated.
To save the data, you would use some kind of serialization to store your objects in a file. When loading, you let the Serializer rebuilt your class structure and (best case) you are good to go.
This is not exactly what you asked for, but I think it is something you could use well ;-)
N.B.: Not the complete state of the control is saved. e.g. in a Textbox your text would be saved but the BackColor won't.
To get you started look into this tutorial: http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial

GUI for XML source- strategy for syncronization between XML editor and GUI

The program I am writing is simple. There is an XML source file-the users need a GUI for changing the details and they need the ability to edit the XML file also. So the program will have two tabs-one is a GUI and the other an XML editor. If the user changes something in the GUI it has to be reflected in the XML editor and if the XML in the XML editor is changed it has to be reflected in the GUI too.
I used XSD.exe to generate the classes for the XML and tried serialization and deserialization on tab changed event. Though it works I am finding the lag(becasue of serilaization/deserialization) while changing the tab a little annoying. Is there a better way to do this?
If it were me I would start by using an XmlDocument or an XDocument as the data source for your object model used by the GUI.
Here's a similar question with a great answer showing this type of concept.
Creating a WPF editor for XML file based on schema
You'll still have to handle the tab/view switching to save the xml file or reload it. But with the data binding a lot of the work is done for you once you setup your object model.

Populating Menu from a database

I need to populate an ASP.NET menu control with hierarchical structure with menu items that can be constantly changed, from a database (categories with n levels of sub categories).
There are some approaches for that and I would like to hear which one is the most efficient one.
I have those in mind:
Retrieving data from database and converting it to xml then transforming it with customized XSLT file and binding it to Menu control
Retrieving data from database and while looping through (recursive), inserting menu items and children to the menu control
SQL Site Map Provider (thanks to Made4Print)
Something else?
The ASP.NET Menu Control can use a .SiteMap file through a SiteMapDataSource.
You can implement your own SiteMapProvider, this way you can have your SiteMap heirarchy within your database and wireup the same components making things more dyanamic.
Here is an example: http://weblogs.asp.net/scottgu/archive/2006/01/11/435108.aspx
HTH
I agree with Marc. You could then package everything inside a server-control for reusability. Converting everything into XML and then using XSLT to somehow "convert" it back seems an overhead to me.
If you want to do this, I would definitely recommend option 2, since it contains one level of transformation less than option 1. If you already loop through the menu items and their hiearchies, you might as well build up the menu items and subitems directly - I don't see any big benefit from taking a detour over XML and then through XSLT into a menu structure, really.
Marc
PS: Option 3 (the SQL Sitemap provider) also sounds like a really good idea, if the site map structure and options are good enough for you (they usually should be). I would probably try that option first, and go from there.

Categories

Resources