Luuk Krijnen
Luuk Krijnen

Reputation: 1192

Using an XML Schema Definition to export data

From one of our partners I got some XML Schema definition files (*.xsd).

are there any good tutorials on how to use visual studio to generate an XML file using the xsd file with the data from our databases.

Upvotes: 1

Views: 3147

Answers (3)

SpruceMoose
SpruceMoose

Reputation: 10320

You will need to generate some c# classes from the schema and then performm some kind of data mapping operation to extract the data from you db and map into these classes.

To generate the classes you can use the XML Schema Definition Tool (Xsd.exe). An example of this might be as simple as:

xsd /classes Schema.xsd

See XML Schema Definition Tool Examples for further examples usage.

Once you have these classes you can extract the data from your database, map to these classes and then serialize to xml using something like the below:

 XmlSerializer ser = new XmlSerializer(typeof(MyObject));

 using(StringWriter sw = new StringWriter())
 using(XmlWriter writer = XmlWriter.Create(sw))
 {
     ser.Serialize(writer, myObject);
     string xml = sw.ToString();
 }

Upvotes: 1

granaker
granaker

Reputation: 1328

You can generate classes for the xsd file with xsd.exe, and then serialize those classes to xml files:

http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx

"Generates runtime classes from an XSD schema file. The generated classes can be used in conjunction with System.Xml.Serialization.XmlSerializer to read and write XML code that follows the schema."

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

Upvotes: 0

khellang
khellang

Reputation: 18122

You could use Microsoft's XML Schema Definition Tool (xsd.exe) to generate C# classes, populate the classes and then use the XmlSerializer class to serialize the objects to XML.

There are plenty of tutorials for both using xsd.exe and XmlSerializer. Google it :)

Upvotes: 0

Related Questions