Convert WinForms design to XML - c#

I'm building a generic survey engine where end users can generate and design their own surveys (including UI design). I'm interested in building (or perhaps even buying) a custom WinForms designer which would be able to output design in XML format. I'd then save the XML to database and when users load a survey, XML would be parsed and the form would be dynamically generated based on the XML data.
Building my own C# to XML converter is an option, but I'd like to avoid it since users would have to use Visual Studio to get ahold of WinForms code and then run a custom tool to generate XML output, which is a pretty clumsy solution (besides I can't force customers to buy VS).
I'd appreciate it if anyone could point me in the right direction.
Thanks!

The bits of the WinForms Designer are exposed by the .NET framework: Design surface, toolbox, property grids,...
This MSDN article and this CodeProject article should help you get started.
Of course, this is only the start of the story.
Your second problem is to make the designer emit XML rather than C#/VB code. Last but not least, you'll have to create the form at runtime, which is usually done by compiling the code generated by the designer. Needless to say that compiling XML is another story.
It would certainly be easier to store the C# code in your DB rather than some XML model. You can then compile it programmatically at runtime (no need to have VS installed on the client's computer. The .NET runtime is enough) and instantiate the form class dynamically. BTW, whether you complie the code in the designer or in runtime is up to you.
However, other routes may be easier, such as Hans' suggestion: InfoPath.

The Mono project has a Winforms Designer. I don't know how easy it would be to re-use as a componant. Here it is in action:
I don't know how you would go about serializing the output to XML and deserializing it again. Maybe you could even store the C# in your database?

Check this post on how to create xml files in C#: Working with XML
Then use sp_xml_preparedocument/sp_xml_removedocument in SQL to create/parse xml files (use stored procedures to do this)
Traverse and use the generated xml in your winform (I suggest you use LINQ to XML to do this)
Probably the hardest thing you'll encounter here is the creating xml part and traversing the xml nodes, but aside from that everything will be a breeze.
We're already implementing this structure/architecture at work but on webforms not winforms. But I think the flow/logic is the same. HTH.
Edit: This post might help you. Cheers.

Related

can C# read in a xml file to change its gui at runtime

I am making an application that will generate and SQL scripts from a template and after taking input for different fields from the user.
There are many templates, so the GUI needs to adjust for the fields that the user will be filling out.
In the interest of keeping this scalable, I'd rather not hardcode the GUIs into the program, but would like have it read from an XML file and change based on the template the user has selected.
This is preferred because if a new template were to arise, then all that the program needs is a XML file that corresponds to the template. And the actual code does not need to be changed.
I have my eyes set on using C# for this, as I have good experiences using it.
I am open to suggestions for other languages though.
Edit: This is a project for work, and I wanted to be sure that this is possible with C# before convincing my employers to expand into using C#.
You could do this sort of thing by subclassing Windows.Forms.Form and adding a constructor to accept your XML file as a parameter. Add a parser for your XML file that will interpret instructions for which labels and fields you want to add to a consistent form design (say, two columns with a label for field name on the left and the actual input field on the right, easily achievable by filling the form with a TableLayoutPanel). You just need to lay out your design constraints from the beginning and stick to them.
This is essentially what visual studio does when you create a form through the designer anyway, so I'd suggest you start by creating an example form manually and just looking at the kind of code it places in the form's designer.cs file
Any language can do that. It's more about design patterns than specific technologies.
If you meant writing a GUI only declaratively and with XML, though, then no. You'd have to write your own parsing and GUI assembling code.

Translating comments and region names in source code

Does anyone know of a batch processor or a VS 2010 plugin/script that would let me translate comments and region names from Chinese into English?
The only ones I've found either process all strings or only one string at a time.
I have two large C# projects that I am trying to read through.
Thanks.
Use PrepTags to prepare your file for translation. It will allow you to select the text to be translated based on regex.
www.preptags.com
You can work file by file for free, or process the files as batch using the pro version (€39)
In your case, it's pretty simple to prepare. You just mark everything as protected, then unprotect the content of the comments & region names.
Disclosure: I develop PrepTags.
As was noted, you can use Google Translate API or alternatively Bing Translator API. You can detect comments and regions in your files using System.CodeDom.
I'm not too sure if this is possible. What you can do to help would be the following:
1) Make sure that both C# projects have the Properties > Build > Xml document file check box checked.
2.1) Write an application that reads in the generated xml file.
2.2) Parse the file, and for each value make a call to Google Translate to get the translated value.
2.3) Place the translated value within another xml file that has the same structure as the one created from building the project.
This wouldn't solve the your desire to translate the region names, but its a start. At least you would have intelligence when using the two projects.
This is actually a good idea for a small open source project. I may decide to pick it up. If I do, I'll let you know.

Dynamic form creation from XSD in .NET

I know there is a lot of documentation on the internet as far as XSD to forms, but I have not been able to come across one that is straight forward enough for my situation.
I am working with a WCF web service that is going to fetch and .xsd xml schema, and must return the HTML of a form based on the .xsd xml schema. Is there any third party tools that can help out with this, if so what are they? If not, do you have any suggestions,better methods,etc for how this can be done?
I am working on my own project called XsdFormEditor. Application will be able to show any XSD as a form in WinForms (95% done), Web (using Asp.Net MVC, 0% done), WPF (0% done). Idea is to load any XSD file, show it to user as a form, let user input some data and save data as XML. User will be also able to load default values to form from XML file. You can find it here: https://github.com/janstafa/XsdFormEditor
Xsd is difficult to parse, but you can easily obtain a class from it, by calling xsd.exe /class or by doing it runtime following this beautiful sample:
http://mikehadlow.blogspot.com/2007/01/writing-your-own-xsdexe.html
hence with the classes representing your xsd you can use reflection to create the html code.
First of all, they will need not only to pass you the XSD (with no include, import or redefine elements in it), but will also need to tell you the root node.
Second, I would approach this by reading in the XSD with the XmlSchema.Read method. You could then loop through the object model pulling the information you need.
You might even try writing out an XML document containing the parts of the schema that you really need, and then running an XML Transform against it to produce the HTML.
Also, consider the Microsoft Office InfoPath product from Microsoft. Even if you don't wind up using the product itself, get a trial version to see how forms can be defined based on XSD plus other metadata.

How do I serialize a .NET control to CODE?

I want to create a .NET Form at runtime, add buttons and other controls to that (also at runtime), and then be able to generate a something.designer.cs file from that form (which can then be added to a C# solution and compiled).
What I want to do is very similar to what the WinForm designer does. But instead of having a drag/drop interface for the user, I want to dynamically build the Form/Controls myself at runtime.
I was thinking I could just reuse what the WinForm designer is doing.
Is that possible?
This MSDN magazine article should have everything you need.
It's really not as simple as it was pre-.NET as the visual version of the form you see in Visual Studio is actually the result of multiple files.
But in the simplest form you could simply just mirror what .NET does at the start of creating a new form:
Create three files Form.cs, Form.Designer.cs and Form.resx (which is an XML file).
Place the same default content in them that VS does
Mimic the code generated when adding controls, code-behind and resources
It will be a tedious task, but it can be done. Adding resources however will be burdensome.
Yes, you can do achieve this using Compiler Services (compiling c# code) or Emit class if you know building correct MSIL.

C# XML language file

I want to have my ASP C# application to be multi-language. I was planned to do this with a XML file. The thing is, i don't have any experience with this. I mean how, do i start? Is it a good idea to store the languages in an xml file? And how in the code do i set the values for ie my menu buttons? I'd like to work with XML because i never worked before with XML, i want to learn how to deal with cases like this.
You want to look into RESX resource files. These are XML files that can contain texts (and images) and they have standardized handling of localization/translations.
Support for this is built right into ASP.NET. There is a guide for how to use it and set it up at: http://msdn.microsoft.com/en-us/library/fw69ke6f(VS.80).aspx.
The walkthough is pretty detailed and should help you to understand the concepts. My preferred is method described a bit down in the document in the section "Explicit Localization with ASP.NET". Using this you will get a set of XML files with your texts and translations in a fully standardized format.
Do you know about the .Net From automatic translatation (based on .resx) resources ?
You're in luck, this sort of stuff is built directly into .Net
The way it's done is that for every page you have a language specific resx file.
eg
Homepage.aspx
Homepage.aspx.cs
Homepage.aspx.en.resx
Homepage.aspx.fr.resx
you simply dynamically figure out what resource file to use, and all the appropriate labels come through in French for example.
Helpful Tutorials and Videos
A Simple Example
Good luck.
If internationalization in .net is something you want to get into seriously, you might want to consider this
(and no - I have no stake in it)

Categories