Zuuljin

Will the next Visual Studio (w/LINQ) be able to generate classes based on an XML document, similar to how it can generate classes based on a database

Thanks in advance!


Re: LINQ Project General Generate Classes based on XML Doc?

jwooley

SqlMetal can already generate entity classes based on an XML based mapping file (dbml). From the LINQ to SQL Whitepaper,

"To instruct SQLMetal to generate a DBML file use the tool as follows:

SqlMetal /server:.\SQLExpress /database:Northwind /pluralize
  /xml:Northwind.dbml

Once the dbml file is generated, you can go ahead and annotate it with class and property attribute to describe how tables and columns map to classes and properties. Once you have finished annotating the dbml file, you can generate your object model by running the following command:

C#

SqlMetal /namespace:nwind /code:Northwind.cs Northwind.dbml"
Using this methodology, you can customize the data mappings in the dbml file and generate the classes dynamically as necessary. Naturally, you do not have the flexibility that some code generation systems offer because you can't fully customize the generated code as you may be able to do with CodeSmith or other code gen systems.
Jim Wooley





Re: LINQ Project General Generate Classes based on XML Doc?

Miha Markic

Just in case you want to play with CodeSmith here is a good starting point for you: my templates for DLinq (yeah, they are old, but should do for start).




Re: LINQ Project General Generate Classes based on XML Doc?

Matt Warren - MSFT

The code generation tool built into SqlMetal is designed to generate the code we *think* you'll want. However, using SqlMetal to generate code is not required to use LINQ to SQL. You could write the classes by hand, being an minimal as you want, or use another code generation tool like Miha has done. This was the purpose of the DBML file. It offers a common 'model' of the domain objects and data context and allows for potentially mutliple designers and multiple code generators. Howeve, there currently is no way to plug-in a different code generator directly when using the LINQ to SQL designer. You'll have to resort to a second manual step after using the designer to call out to a different code generator.






Re: LINQ Project General Generate Classes based on XML Doc?

Zuuljin

Thanks for the replies. I think a tool like CodeSmith is probably what I'm looking for... I was just hoping there was a way to use the LINQ designer in the new visual studio. Perhaps drag an XML file to it, it generates the class mapping. Similar to dragging a db table to it generates the mapping.

Thanks again!




Re: LINQ Project General Generate Classes based on XML Doc?

Patrick De Block

I think the guys above misinterpreted your question. What you are looking for is probably

Linq To XML. Haven't used it myself, but I guess you can generate some kind of object model

for XML file contents similar to what linq to sql does for a database table.

kind regards,

Patrick De Block





Re: LINQ Project General Generate Classes based on XML Doc?

Matt Warren - MSFT

You've been able to generate an object model from XML schema since .Net 1.0. The XSD.exe tool will do that. Of course the objects generated are useful only for serialization. You can feed the XSD.exe tool either an XSD file (if you already have one) or a XML file from which it will 'infer' a best-guess schema from the data. XSD.exe is part of the .Net SDK and is probably on your machine somewhere if you have Visual Studio installed.




Re: LINQ Project General Generate Classes based on XML Doc?

Zuuljin

Thanks, I'll try it out. I'll respond again once I've had a chance to mess around with xsd.exe.