In this example i am using VS2010 with .Net 4.0 as the framework.
The XML i am using is:
File.html List.html Data.html A4
Located in VS2010, and most likely VS2008 is the CreateSchema option in the XML menu, this becomes available when editing XML documents.
Using create schema on the above XML sheet would create a XSD document as below:
Using the XSD tool (Found in C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\xsd.exe with VS2010) it is possible to create a LINQ query-able Dataset/Class from XML.
Running the following from a CMD prompt will generate a class file that you can use in your application:
“C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\xsd.exe” C:\_DEVELOPMENT\Your.xsd /c /eld /edb /o:C:\_DEVELOPMENT\
What the command line parameters mean:
- The Location of the xsd.exe
- The Location of your xsd file
- The Arguments:
- /c indicates we are creating a class.
- /eld indicates we are creating a dataset that can be queryable by LINQ
- /edb indicates we are to allow data binding
- The destination of the class file, in this instance it will be called Your.cs and placed in the C:\_DEVELOPMENT\ dicrectory.
Import the Your.cs class into the project you are working on.
I Have used all the following namespaces, plus the standard ones for a application/webapp:
using System.Diagnostics; using System.IO; using System.Security.Permissions; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; //The following function can de-serialze the ‘Convert Object’ from an XML document public Convert GetConvert(string FilePath) { Convert res = null; if (System.IO.File.Exists(FilePath)) //Check to see if the file is there { using (FileStream fs = new FileStream(FilePath, FileMode.Open)) // Load the file into a file stream { using (XmlReader reader = XmlReader.Create(fs)) // Create an XMLReader from the { XmlSerializer serializer = new XmlSerializer(typeof(Convert)); //Prepare a serializer, with the Convert Object type Convert deserializedconverts = serializer.Deserialize(reader) as Convert; // Deserialize an object res = deserializedconverts; // * return the deserialized object. } } } return res; // * return the deserialized object. } //The following function can serialize a ‘Convert Object’ to an XML Document public bool WriteConvert(string FilePath, Convert input) { try { using (FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate)) // Open or create a file named by the filepath param. { XmlWriterSettings writerSettings = new XmlWriterSettings() { Encoding = new UTF8Encoding(false) }; // set the encoding to UTF8 using (XmlWriter writer = XmlWriter.Create(fs, writerSettings)) // Create a new xml writer { XmlSerializerNamespaces names1 = new XmlSerializerNamespaces(); names1.Add(“”, “”); // Remove any silly microsoft namespaces XmlSerializer serializer = new XmlSerializer(typeof(Convert)); // create us a serializer serializer.Serialize(fs, input, names1); // serialize the object. } } return true; // if it worked return true } catch (Exception ex) { return false; // if it didnt work return false } }
That’s essentially it, You may have to add references to both the System.Xml.Linq and the System.Data.DataSetExtensions if you get build Errors.