WinForms - Custom XML Editor

一世执手 提交于 2019-12-11 12:32:02

问题


I've created a small HTML/JavaScript based mini-web app that reads data from an XML file.

Now, I need to create an offline WinForms editor for this XML file. I'm fully aware that using an XML file to store data in the fashion that I do is far from ideal, but the requirements are such that I can only use static files for the web site, though the XML file can be replaced when it is updated.

The mini-web app allows a customer that is buying a remote car starter to select the make, model and year of their vehicle and be shown a list of additional components that are required for the installation. This data is represented in XML as a <vehicle> element with attributes representing the make, model, year, as well as attributes for each of the types of components.

The requirements for the editor are:

  • Display vehicle records in a grid
  • Allow autocomplete in each column based on values already entered
  • Allow row filtering based on any combination of columns

I've tried the following:

  • Using DataGridView and databinding, but databinding/datasets/etc don't seem to want to play nice easily with XML files as the data source. I gave up after wrestling with this for several hours
  • Creating my own custom "row" control and inserting an instance for each element into a Panel control. Since there are several hundred vehicle records, this is a non-starter for performance reasons.

I'm generally a web guy, so this WinForms stuff is uncharted territory for me. What is the easiest way to accomplish the requirements for this editor?


回答1:


Rather than creating a UserControl that incorporates a large number of smaller UserControls for representing each element, I think the sanest approach is to encapsulate the logic for visually representing the elements as classes that are responsible for rendering their data onto a single graphics surface (as opposed to encapsulating the logic in UserControls).

Since you've already created a "row" control (with editing capabilities, I presume), you can still make use of this by creating an instance of it and "floating" it over your control when the user clicks on it.

This earlier answer to a somewhat similar question:

Need help creating control to display data

shows the basic principle. You could use this approach to edit extremely large XML documents with only two controls being instantiated at any one time.




回答2:


You could consider reading the XML file into memory and creating a typed dataset based on that XML file. If you have control over the format of the XML file, you could start with a typed dataset, serialize it to XML to get a structural template, then work the other way from then on... edit the XML file and load your dataset by deserializing from the XML file.

This article discusses typed datasets and DataGridView, and also provides great examples of how to filter input.

Finally, you may want to have a look at the free Krypton Toolkit. Their controls are generally more powerful than the equivalent out-of-the-box Winforms ones they replace. I believe they may have auto complete implemented for DataGridView (they do for TextBox for sure). If you use it, you will actually download both their free toolkit and their commercial tools. Make sure you only reference the toolkit DLL unless you intend to purchase their full offering.



来源:https://stackoverflow.com/questions/1482353/winforms-custom-xml-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!