I am trying develop a website which provide user interface to generate XML file. The user interface will ask for data required in various XML elements. The generate XML should follow DTD specifications.
So here is what I did.
I converted DTD to XSD.
I created C# class using xsd.exe tool.
Now my question is how can I generate dynamic input boxes on the webpage that will ask for required element data from the C# class I created.
I need some way to know the required and optional elements and their data type and attribute and all from the C# class I created.
i hope you get what i am asking, thanks for looking.
Keep in mind that most of the required/optional semantics from the xsd are lost in the classes generated with xsd.exe. You basically have 2 (+1 edited later ) options:
Use reflection over your generated types to render UI elements for each property. You'll have to manually manage/define databindings
Drop the xsd.exe classes and generate your UI elements by traversing the xsd itself. That way you get way more info about optional/nullable elements, cardinality etc. Construct your resulting xml by hand (use XDocument) from your UI inputs.
The hybrid approach: Reflect over generated classes for structure (easier traversal logic. no need to handle external includes etc). Go to the xsd for the additional info (You'll need to somehow figure out where in the xsd to find your needed definitions that map to the current property)
Either way you choose this will not be a trivial task and you'll need a lot of work to make it happen. And if we're going in the realm of XSD choice elements etc. you'll soon figure out that no straight forward UI can cover all the possible scenarios
Related
Initially I had various XSD definition for each XSD I had set of XML files stored.
After some time duration there are some changes in XSD definition so my stored XML is no more validation again new XSD.
For support I need to write XSLT and do changes in my stored XML to validate again new XSD.
Now, in this scenario each time XSD change, I need to write XSLT manually how can I generate this XSLT dynamically.
Currently I am able compare old and new XSD and get the list what is changes using Microsoft.XmlDiffPatch DLL.
Based on this changes I need to generate XSLT using C#.
I don't know what your question is, but I think this is technically possible.
It might be easier to just write some c# code that reads the Xml and then augments it and sets it back to the file/database/dataStore.
Not sure there is a magic bullet for this. Sounds like you're in for some work, and I'd advise that whatever you do be as reusable as possible in the face of future changes.
You might want to consider using xproc (via Calabash or some other engine) to create an XML pipeline whereby you detect and pass in changes of an XSD into an XSL (perhaps keeping to the convention of one XSL per XSD, to retain your sanity), and then said XSLs take those changes and handle them for all XML files bound by the XSD whose changes are being handled at the moment. Breaking all these into sub-transformations within the pipeline could be possible, and might make things more reusable in the future.
Inside the XSLs you're likely looking at doing something like:
for all changes to be made
for each XML
match/add/delete per element and/or attribute to implement change
One way to represent the changes to be made in some sort of standard format is as an incoming list of operations to perform and associated elements/attrs to act upon (maybe set it up as key/value pairs). Each operation could be a string (add, delete, convert) or a numeric code. You then traverse the list of ops and associated elements and trigger matches to accommodate.
This is all somewhat abstract because I have no idea of the scope or depth of changes you need to make. I'm really just thinking out loud here. You might just have to knuckle down and do some serious one-time work, then implement some sort of change control process to make sure things don't get out of hand in the future.
Hope this helps. Good luck!
assume this - I want to read and write XML in a predefined scheme - till now it's ok.
The thing is that there is explicit action a developer needs to do case he wants to add another attribute (xsd.exe for example) and it's quite frustrating in teerms of source control (need to check out, generate new file, and check that in).
Is there a way to read xml just by supplying its XSD (without the need to actually auto-generate a class for that?) - in that case if new field will introduce - developer will change only the XSD or something?
Please check the below link,
This can be the solution for this problem.
But please keep in mind that using xsd.exe is robust way to generate the classes for schema.
You can generate the class code form xsd on the fly and you can compile the code and use in your application
How to programmaticly generate .NET classes from XSD? (Like xsd.exe do)
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlcodeexporter
http://msdn.microsoft.com/en-us/library/650ax5cx.aspx
I have an XML file which roughly describes a database schema I am inheriting
I want to generate POCO objects for this file to give me a head start with the business objects in my C# application.
Is this possible and how?
You could (and should) define a xsd which describes your XML file. From this XSD you can generate classes using xsd.exe.
If you need more control over your code generation (e.g. you aren't happy with the output of xsd.exe, want to add attributes, make changes, ...) you can use System.Xml.Serialization.XmlSchemaImporter, System.Xml.Serialization.XmlCodeExporter and CodeDom to adjust the generated code.
Yes,it can be done. Take a look at T4 text templetes
http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx
Try SimpleXmlToCode .It does not require an XSD or anything. It generates good serializable code instantly.
Best of all, it's opensource.
When I am editing an XML document that has an XmlSchema, how can I programmatically determine the elements that can be inserted next? I am using C# and I already know which element I am in. Is there an MSXML method I can call or something else? Thanks.
Sounds like you are after the .Net Schema Object Model (SOM)
Schema Object Model
Here is an article on how to work with the SOM.
Example 1
Tarzan,
As I understand it, you are trying to determine the legal XML that can be added at a specific place in the document, based on the schema being used. If that is correct, it is a very difficult problem to solve. If you have an "any" element in your XSD, your complexity increases because you can literally be any element! Also, XSD schemas can be subclassed (i.e., an element definition structure based on another structure), then that introduces more complexity. There are only couple of products (Oxygen, Visual Studio) that have attempted this with any success (that I know of).
If your schema is fairly simple, and doesn't include any of these deal breakers, you might be able to use the Schema Object Model to find the legal elements at your current location, but only if you know what portion of the XSD applies to your current element.
Does this make sense?
Erick
Is there a pattern, Xml structure, architecture technique we can use to create simple data holder class code that we can deserialise to/from and do that at runtime?
We're in the early design stage of a .Net project and one of our goals is to make the resulting system extensible through configuration without needing a developer. We need to support new sources of data, typcially delivered as Xml messages. Currently we convert/deserialise the messages into simple classes and then use an already existing language which can manipulate those classes as we need.
That works well when you have a developer to map the Xml to simple class, create the class and then write the deserialisation, but it's not extensible for for an administrator. Our target user audience is high end DBA and/or network admin - people who can handle Xml but may not know C#.
You don't have to write any classes or deserialization routines. If you have a schema, you can use the XSD.exe tool from Visual Studio to automatically make Classes, and use built in .NET XML Serialization/Deserialization.
Now how to have that happen without a recompile each time...
It's not ideal, but this should work:
Assume your DBA can write a schema for the XML.
You could write a tool that takes the schema, runs it through XSD, Add's some wrapper code on top of it, and creates a dll which can be used from within your application.
This could be a manual process (ie the admins email you the schema) or you can distribute the tool as part of your application.
Also, you can infer a schema from an existing XML document.
Perhaps DataTable? Or just use an xml DOM (XmlDocument or XDocument) as data-storage? Neither is ideal, of course - but there is little point creating a type at runtime just for this if your real code will ever see it. What purpose would the extra class type serve? Among other issues you'd have to use lots of reflection just to talk to it.
The other option is a custom property-bag and IXmlSerializable, but that is effort.