Dynamic form creation from XSD in .NET - c#

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.

Related

C# easiest way of storing strings to external file

C#
TL;DR: I want the user to be able to input text, which is then written to an external file, which then can be called later based on position in the file. What's the easiest way and form to read and write a list of strings to?
Very amateur question, but I can't seem to find an easy anwer on the internet. If I'm missing something obvious, please redirect me. I am writing a very simple program in which the user can input a string which is then written (added) to an external file, from which later a string can be called based on the position in the file. I found things like JSON, Resource file, SQL DB... Problem is that due to my lack of programming experience I have no idea what's the best option to look into.
Example of what I want to achieve:
User inputs strings 'Dog', 'Cat', and 'Horse' into the textbox. Each of these strings are added to the external file. Lateron, the user calls the 2nd number on the list, to which the program returns 'Cat'.
Thanks!
If you already know the kind of data that will be saved I recommend using XML Serialization. This lets you save and read your file very easily. The linked example is from Microsoft and shows a dataset being serialized. If you want to save a generic list instead of a fixed object you might find this link helpful.
Alternatively, you could save data to your application configuration file (search online for "C# application configuration for PROJECT_TYPE" where the project type is winforms/mvc/class library etc..)

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.

Convert WinForms design to XML

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.

Loading a XML file and injecting data into a table and return it as a navigation/tableview

So here's the situation: I have a friend who wants me to make an app for his comic website, he wishes for me to set it up so the comics appear in a tab (navigation and tablelist/view). Im not sure what the best method for his XML file is, i do know of the xml code on how do it, but unfortunately, i do not know nor have ever used the code for it.
so im sorta lost, this is the file: http://hoshen.controlshiftblack.com/HoshenFeed.xml
And does anyone have any simple to use suggestions for using XML in an app?
There are saveral ways to get the job done, you can use an xml library and make a parser for your xml feed like
NSXML
http://deusty.blogspot.com/2008/09/nsxml-on-iphone.html
or libxml
http://iphonedevelopment.blogspot.com/2008/04/simplifying-libxml.html
Also there are third party parsers that allow you to use Objective-c objects, one widely used is
GData
http://feedproxy.google.com/~r/RayWenderlich/~3/YuSy-Gey1sg/how-to-read-and-write-xml-documents-with-gdataxml
If that's not enough, ask your friend to send you JSON data instead and use a JSON library for iPhone
http://maniacdev.com/2010/07/using-objective-c-json-to-do-translations-in-an-ios-app/

C# Crystal Reports and XML Dataset (ADO.NET) Data typing problems

Afternoon Developers,
I have a problem that's beginning to get on my wick. I have looked through google and Crystal Reports help and am still stuck for a solution.
We have created an XSD complete with full information about data-types and the like.
A report has been created that is linked to this XSD (using a c# class generated using Microsoft's xsd.exe SDK) and uses the field information to fill the report.
From c# we are instantiating this report and populating it with an XML file that conforms to this XSD and has been validated against it.
Now the problem is that although everything validates without errors (Crystal formulas etc) and compiles fine, when we run the application and attempt to populate the Crystal Report the Report seems to loose all knowledge of the data-typing.
Formulas such as
if ({LetProperty.lp_buildings_selected} = true) then
"Standard Cover With Accidental Damage Cover"
else
"Standard Cover";
come back with "A string is required here." error. This formula should run fine as the field referenced is a Boolean Data-Type, Crystal Reports knows this in design time.
To get it to work we have been needed to cast the field ToText and compare it to 'true'.
This is a simple work around but we will be creating many reports and don't want to twist code that should work into 'Fudged' messy code that will work.
Can anybody tell me if I am missing something glaringly obvious or if there is something that we need to set as this is confusing us immensely!
Many Thanks all.
Right,
I just wanted to let anyone know how we solved this if anyone ever gets stuck in the same situation.
What we needed to do was before populating the created dataset from the Xml file (to pass to the Report) we read the Xsd into the Dataset so that it was then strong typed and contained all the Data-typing information also.
I don't know why crystal doesn't keep all the Data-Type information that was assigned to it when we created it and linked it to the Xsd but this is how we got around that.
Is LetProperty.lp_buildings_selected set to anything besides "true" or "false" in the XML document (perhaps empty string)? If so this may be the reason that Crystal Reports is trying to use it as a string.
I believe the generated XSD is probably set up by the prevent that, but thought I'd throw it out there anyway.
Is the problem just that you need to include the schema in each XML file? Doing this will have the full type information sent to Crystal as you tell it the XML file is the DataSource. I think I had a problem like you mentioned when I did not include the XSD in the header of the XML file.

Categories