Store xml in C# class field - c#

I have a requirement like i need to write an entity class in C# which can hold xml data.
I want to avoid overhead of checking the well-formedness of saved xml.
I have a corresponding column with type XML. Do we have xml data type or some class which can be used as a class field to hold xml.
Thanks in advance
Update: The service using this Entity class is WCF service and in future we are making it REST compatible. Will XmlDocument or XElement work with it?

There are a number of ways, two of which are string and XmlDocument.
string would be 'easier' for fragments and not-well-formed XML, but XmlDocument can be configured with options to allow fragments; you'll have more trouble with ill-formed data though.

if you describe your object in a XSD file you can get a compiler to generate all your C# classes automatically and easily regenerate them when you make changes.
This makes XML / C# a breeze. You can go to other languages too using equivilent generators.
See the tools described here: XSDObjectGen.exe vs XSD.exe
I believe the XSD.exe tool will read in example XML and do most of the work of producing an XSD which you can refine.
If you don't need support for a fixed XML/XSD format file why can't you make any class serialize to XML by using the [Serializable] class attribute and .Net APIS to serialize/deserialize?

Related

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.

what is the best way to convert a class to xml and vice versa

what is the best way to convert a class to a XML and vice versa?
I want to convert my class to xml for using XRule and I want to convert back it to class with early type of properties.
thanks
You can use XML Serialization to easily go from a class to XML, and the reverse. This tutorial does a good job explaining how to do it, and how to finely craft the resulting XML.
Try the XmlSerializer class: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
XRule sounds like it plays a similar role as an XML Schema (I'm not familiar with it). The XML Schema Definition (xsd.exe) tool allows you to generate common language runtime classes from XML files and vice-versa. More info can be found here.

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.

DeSerializing an XML file to a C# class

Does anyone know what advantages (memory/speed) there are by using a class generated by the XSD tool to explore a deserialized XML file as opposed to XPATH?
I'd say the advantage is that you get a strongly typed class which is more convenient to use, and also the constructor for the class will throw an exception if the XML data in the file is invalid for creating the object, so you get a minimal data validation for free.
If you don't want to write boilerplate code, and you need to check ANY values of your XML on the way through, you can't go wrong with the XSD.exe generated classes.
The two are very different; but XmlSerializer will always deserialize entire objects; with XPath you can pick and choose. I'd use XmlSerializer personally, though - harder to get wrong.
XPath, however, is a complex beast that depends on the back-end. For example, XmlDocument (mutable) will behave differently to XPathDocument (read-only, optimized for query).

Xml Serialization and Schemas in .net (C#)

The following questions are about XML serialization/deserialization and schema validation for a .net library of types which are to be used for data exchange.
First question, if I have a custom xml namespace say "http://mydomain/mynamespace" do I have to add a
[XmlRoot(Namespace = "http://mydomain/mynamespace")]
to every class in my library. Or is there a way to define this namespace as default for the whole assembly?
Second question, is there a reason behind the always added namespaces
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
even if there is no actual reference to any of the namespaces? I just feel they add noise to the resulting xml. Is there a way to remove them an only have the custom namespace in the resulting xml?
Third question, are there tools to support the generation of schema definitions (e.g. for all public [Serializable] classes of an assembly) and the validation of xml against specific schemas available?
If there are, would you recommend XML Schema from W3C or RELAX NG?
Just to add - the "xsi" etc is there to support things like xsi:nil on values later on - a well-known pattern for nullable values. It has to write the stream "forwards only", and it doesn't know (when it writes the first bit) whether it will need nil or not, so it assumes that writing it unnecessarily once is better than having to use the full namespace potentially lots of times.
1) XmlRoot can only be set at the class/struct/interface level (or on return values). So you can't use it on the assembly level. What you're looking for is the XmlnsDefinitionAttribute, but I believe that only is used by the XamlWriter.
2) If you're worried about clutter you should avoid xml. Well formed xml is full of clutter. I believe there are ways to interract with the xml produced by the serializer, but not directly with the XmlSerializer. You have much more control over the XML produced with the XmlWriter class. Check here for how you can use the XmlWriter to handle namespaces.
3) XSD.exe can be used to generate schemas for POCOs, I believe (I've always written them by hand; I may be using this soon to write up LOTS, tho!).
Tools,
- xsd.exe, with a command line like
xsd /c /n:myNamespace.Schema.v2_0 myschema_v2_0.xsd
I put the schema in a separate project.
liqudXML which is useful if there are several schemas, or you want full support of the schema features (DateTimes with offsets, positive/Negative decimals,), and cross platform generation.

Categories

Resources