John K
John K

Reputation: 28897

Generate LINQ to XML C# code, from an XML document?

Does anybody know of a tool that will generate LINQ to XML code from a real XML document or fragment? It's reverse-engineering the common scenario of generating XML.

For example, I want to provide an XML fragment as input like this

<root>
    <thing>value</thing>
</root>

and have it generate the equivalent C# LINQ to XML code snippet like so

var x = new XElement("root",
    new XElement("thing", new XText("value"));
    );

Although I'm looking for a quickie, I'm sure some enterprising individuals will tell me to roll my own and provide some awesome reference code.

Upvotes: 4

Views: 1699

Answers (2)

Shahin
Shahin

Reputation: 12841

See this tool.

the application supports :

  • XDocument
  • XDeclaration
  • XProcessingInstruction
  • XComment
  • XNamespace
  • XElement
  • XAttribute

  • generation of business objects

  • generation of code Linq To Xml (with variables, in method, extraction of the code corresponding to the selected nodes)

  • you can open a Xml file or directly copy to stick xml in the richtextbox

  • the editor allows to create Xml documents from scratch or to add/modify existing Xml documents
  • the editor has several views which are synchronized (Text, treeview)
  • a help with the seizure (auto completion tags and attributes and checking in the course of the good formation of xml) for the text view,…
  • you can also post the data of the nodes selected in a datagridview
  • etc

Upvotes: 6

Andrew Cooper
Andrew Cooper

Reputation: 32586

This wouldn't be hard to do using T4 templates, or an XSL transform for that matter, but I don't know anyone who's done it.

Upvotes: 0

Related Questions