How to Generate XSL & XSD from an DataContract object - c#

i have a peice of code that outputs many different XML files using the DataContractSerializer
i would like to be able to output an XSL and XSD along with each one.
this is purely for learning purposes.
Also I have not included any code as i think it is a fairly generic question.
the only way i have found to do this so far is by using the svcutil.exe.
Is the datacontractserialiser able to do this at runtime?
(or is my understanding of the XSD and XSL incorrect?)
Having done a bit more reading i understand that the XSL needs to be defined for the XML not along with it and is something that needs to be done by me manually.
Edit: I have the Svcutil working i was however wondering if it was possible to do this in code. (ultimately i would like to place a copy of the XSD in the same place as the XML file)

You can extract the XSD from the DataContract using svcutil.exe
svcutil.exe /dataContractOnly *.dll
Docu:
http://msdn.microsoft.com/en-us/library/aa702581.aspx
Edit:
To do this at runtime use the XsdDataContractExporter

Related

What is the proper way of creating deserialization C# classes from XML data?

I'm on a project where C# deserialization classes appear to have been generated based on XML files, rather than directly from system XSD's.
Can someone please confirm: An XSD created from this single XML file could be missing metadata as some properties may be either null or missing, depending on how the XML file is generated.
From what I understand, the proper way to do this is to generate classes directly from system XSD's rather than from XML files, and identify some issues when generating from XML's?
You are right. Optional elements or fields that are missing from the sample XML file will not appear in the generated XSD file and thus not in the deserialization classes. This is also the same for a single item appearing in the XML where a list of items was expected. Even if you have a full XML file it might be missing constraints, etc. you would have in your XSD.
My suggestion is to always start from the XSD. Then create a test XML file to test against it.

Using a .NET XMLWriter with XSD

I've been given the task of writing a complex XML file (I do have the XML schema, XSD) in C#, which has the possibility of being quite large depending on the situation. I'd like to implement streaming since the file can be large, so it looks like the best option is to use the XMLWriter. Before I go down the path of extending the XMLWriter class and writing a bunch of custom code, I was wondering if it was possible to, somehow, leverage the XML schema I have? I know I can convert my schema to C# objects using the XML Schema Definition Tool in Visual Studio, but I don't know if this is something I can use with the XMLWriter. I've converted an XML schema to C# objects and serialized them using XMLSerializer in the past, but not with the XMLWriter.
See Generating XML Documents from XML Schemas
http://msdn.microsoft.com/en-us/library/aa302296.aspx
Summary: Priya Lakshminarayanan shows how you can use the classes in the System.XML.Schema namespace of the Microsoft .NET Framework to build a tool that generates sample XML documents that conform to a given schema.
In the Visual Studio Schema Explorer you are able to generate an instance document from any element definition in your Schema. This article exposes the underlying code that makes that happen. I should note that Altova's XMLSpy has a more flexible tool for generating instances from the Schema, allowing you to set various parameters about the depth, repetition, and generated text values.
I used the XMLGenerator code included in the article to create a class that generates new XML document instances from my Schema for the 20 types of documents that we define. I added hints in my Schema as attributes in my own namespace to help the XMLGenerator generate a minimal valid document with some default text to help the users get started with the new document. So there is a lot you can do with the XmlGenerator.

How to get cs files for xml schemas (xsd)

I'm new in C# developpment and I want to know the procedure to get one CS file for each XSD schema I have.
I have 3 xsd files (A.xsd, B.xsd and Common.xsd). Both A and B have Common as import. I've tried "xsd.exe" and "xsd2code.exe" without success.
"xsd.exe"
Cannot generate classes because no top-level elements with complex type were found into "Common.xsd".
Files "A.cs" and "B.cs" are generate but the code of "Common.xsd" is in each files.
"xsd2code.exe"
"Common.cs" is well generated with the namespace I've choosen, "MyNamespace.Common".
Other files could not be generated because of unspecified types however I use the "MyNamespace.Common" as custom usings ("/cu" option).
Does someone have an idea?
Thanks in advance.
You need to reference all the schemas that are in the import or include chain.
Try this:
xsd /c A.xsd B.xsd Common.xsd
Edit: However this will mean that all the classes are in one file.
If you want them split out into different files you have two options:
Manually split them out. I know this is a generated file but if your source schemas are fairly static then it's a one off exercise.
Use svcutil.exe instead. However this is much more complicated and actually you may not even be able to do this unless your schemas all abide by certain guidelines. If you are interested see below for the process.
If you fancy option 2 above then here is the general process:
Extract the types from Common.xsd using the /dconly flag on svcutil. This will generate a class file with your common types.
Compile this class into an assembly
Extract the types from A.xsd using the /r flag and referencing your CommonTypes.dll assembly.
Do the same for B.xsd
However, this approach is based on svcutil using the DataContractSerializer to do the work, as the /r flag is not available to XmlSerializer. And this will only work if the your schemas adhere to the rather strict DCS rules (can be found here: http://msdn.microsoft.com/en-us/library/ms733112.aspx).
If these rules are not adhered to then svcutil will fall back to using XmlSerializer which does not support /r flag.
Hope this helps.

C# : creating classes from XSD file using XSD.exe (help with circular reference in XSD file)

I am trying to generate C# classes from the following XSD file.
I run xsd.exe with the options to generate C# classes. Ideally I would like to create an ORM from the XSD (and thus generate the DDL from the schema file), but I am not sure if XSD.exe is the way to go forward.
In any event, this is what I want to do (in decreasing order of urgency)
Create classes from the XSD file
Provide CRUD functionality (active record pattern) via an (autogenerated?) ORM
Autogenerate DDL from the XSD and populate the db with the permitted values specified in the XSD.
For the last one, I suspect that I may have to hack something together using XSLT.
Starting with the first problem, I run xsd.exe like this:
xsd mddl.xsd /c /eld /o: c:\some\folder
The output from running the above command is:
- Group 'mathNode.model' from targetNamespace='http://www.mddl.org/mddl/3.0-beta' has invalid definition: Circular group reference.
Now I am new to XML/XSD etc - so I am currently stuck on how to resolve this. Can someone spot why the Circular reference is being caused - and more importantly, how to fix it?
You may have already seen this; it complains of the same issue, and the answers suggest hand-creating the classes to handle the serialization. Doesn't seem like a great thing to me, but there you go.
I'm running into the same problem with a schema we've been given, and researching other tools that might be able to handle this. There's conflicting information as to whether this is actually valid from an XML Schema standpoint; most people think that it is (though MS disagrees: link).

Generate POCO objects from xml file

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.

Categories

Resources