Exporting to XML from SQL Server(2000) in vb.net (2005) using a xsd as base.


I have a xsd file that defines the format i must use to export data to a XML file.

This XSD is provided for me by the government. (see attachment).

I have used Microsoft's xsd to create the classes based on this XSD schema. Then added the class to the project.

Now, i need to actually export the data to a XML. Using, for example, a data reader to get the data from SQL Server (2000) and then, somewhat, export it to a single xml file. This is the part i am stuck atm.

What's the best approach for doing so in vb.net. ?? Can you provide some code examples?

Thanks in advance for the help.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:
You can use XSLT to transform your data into XML if you want.
nvivasAuthor Commented:
Hi Alfred1.

Thanks for your response. It doesnt help me much tho. I'm new to all this and don't know what you mean by using XSLT.Can you be more specific? Provide some code examples?

Alfred A.Commented:
XSLT - Extensible Stylesheet Languange Transformation

Check this out:


Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Alfred A.Commented:
You can also use XML DOM (Document Object Model) if you want.
Alfred A.Commented:
Here is some basic stuff about XML programming in VB.NET.  DOM and XSLT is also explained.

Alfred A.Commented:
Oh by the way, if you will be working with datasets, you can convert the dataset information into XML text by using Dataset.WriteXML function.  You can also use the Dataset.ReadXML function to read an XML document and load it into a DOM XmlDocument.  From there you can manipulate the information you want to transform it to the desired XML.
nvivasAuthor Commented:
Hello again Alfred1.

Thanks for your responses.

I'm not interested in reading XML but writing.

The link you provide, i have already read. I can understand it and used XmlWriter class to do some  testing.
This approach will force me to implement all the tables manually. What i am looking for is some sort of approach that picks up any given class ( generated from the xsd) and generates the xml automatically.

If i use xsd executable from microsoft, with the /d parameter instead of /c it will generate the datasets for the structure that exists in the xsd file i attached.
This doesnt work very well for xsd's with the complexity of this one. So i hit a wall here.

What i am looking for is something that allows me to, for example, by using a code similar to the one in the article. Then i somewhat use the xsd to correctly format the data returned by the command. For this i thought of using classes (provided by the xsd) and use some generic tool that looked at the structure of the class and exported it to XML correctly.

The point of this is that, if someday they change the xsd, with minimal changes to the code i will be able to perform the adaptation.

I appologise for the lack of English skills.

Thanks in advance,

Protected Sub  Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ds As New dataset
        Dim connStr As String = "database=NorthWind;Data Source=localhost;User id=sa;password=sa"
        Using conn As New SqlConnection(connStr)
            Dim command As New SqlCommand("select * from customers", conn)
            ds.DataSetName = "Customers"
            ds.Load(command.ExecuteReader(), LoadOption.OverwriteChanges, "customers")
            Response.ContentType = "text/xml"
        End Using
    End Sub

Open in new window

Alfred A.Commented:
What is the error you are getting when using xsd.exe?  This tool has help me a lot in most cases in generating datasets.
nvivasAuthor Commented:
Im not getting the error with the tool.

Its when i use the dataset provided by it that i get and error in the class declaration.

When i use this

Dim ALL As New XSDSchema.Namespace._SAF_T_PT

i get the message in PT :(rough translation) : " The same table "Address" cant be subordinated in 2 nested relations"

Should i only use

Dim auditfile As New XSDSchema.Namespace._SAF_T_PT.AuditFileDataTable

And work my way down in the nested tables from there??

Alfred A.Commented:
OK.  In Visual Studio 2008, there is an XML Schema Explorer that you can use to generate an XML.   I am not sure with VS2005, it has been awhile.

You can try the following for creating strongly typed datasets:


Also, you can do this using XMLSpy.
Alfred A.Commented:
Based on your previous comment, yes you can.

Oh, and by the way, why not use xsd /c option?

Check the following:


in the site mentioned above, have a try of this (this is just ugly :-)).

  - use xsd.exe to generate classes for the specified W3C XML Schema
  - compile the resulting source code into an assembly
  - use reflection to instantiate and fill an instance of the first type defined in that assembly
- use XML Serialization to serialize the instance, and produce an XML doc that conforms to the XSD
check out this link for complete code: http://cheeso.members.winisp.net/srcview.aspx?dir=xml-serialization&file=Xsd2xml.cs
nvivasAuthor Commented:
Hey Alfred1

Take a look at the xsd that i provided.

Imagine i want to generate a xml based on the MasterFiles  definition. It has generalledger,Customer,Supplier,Product,TaxTable.

Admiting im using the dataset class generated by the exec tool, how would you do that in code?

I would start with    

 Dim masterfile As New XSDSchema.Namespace._SAF_T_PT.MasterFilesDataTable

and end with


How do i populate, say, customers in the masterfile?
nvivasAuthor Commented:
Looking @ then links you provided i've found something very interesting ( i think!)


You can see:

 1Dim p As New Person()
  2p.Name = "Fred"
  3p.DateOfBirth = New XmlDateTime(1978, 6, 26)
  5Dim a As New Address()
  6p.Address = a
  7p.Address.HouseNo = 7
  8p.Address.PostCode = "WV6 6JY"
 10Dim runAroundCar As New Car()
 12runAroundCar.Model = "Ford"
 13runAroundCar.Make = "Escort"
 15Dim toyCar As New Car()
 17toyCar.Model = "Lotus"
 18toyCar.Make = "Elise"

That's something more(very) close to what i'm looking for.

Can i do that with just VS2005, xsd.exe and MS SQL Server 2000 ??
Alfred A.Commented:
Well, there are a lot of ways to approach this.  If you use the System.Xml framework and use Dataset, XML Document Object Model, XPath and XMLWriter, your code would be large but if you design it to do it in small pieces let say put Customer to Customer.vb class and handle customer there, until all the leaves of the XML are done and then connect all of them.  This would be manageable.

I experienced doing this approach before and it works just fine.  Like I said, there are a lot of ways to do it.  The pros of this approach is a bit convenience because properties or methods are available to help you creating the XML.  The cons is that there is a slight performance penalty hit.

I hope this helps.
Alfred A.Commented:
Answering your comment ID:31278777, my previous comment before is actually what I was talking about. :-)

Yes, I believe you can still use that approach in VS2005 based on what I did with a project similar to what you have.
Alfred A.Commented:
By the way, a bit of warning,  the only bad thing with that approach is that it might make your code really big especially if you have a large XML Schema.

Just make sure you isolate the elements/tables in their own respective classes so that they are manageable.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nvivasAuthor Commented:
Damnit. You are replying toooo fast!! :D

For me to do that, i would need to buy the Liquid software..... Is there a way to produce similar thing using the classes provided by the xsd.exe ??

Do you have any code example from one of your projects?

Take the post nº31277755 example. How would you complete it for it to work???

(i have a meeting now - will come back tomorrow... thanks so much for your effort)

nvivasAuthor Commented:
This didn't quite help me solve my problem. Nevertheless I'm giving the points for the effort Alfred1 showed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.