generate xml document

T

Tony Johansson

Hello!

Is possible to generate an xml document in any way or is the only way to
write it manually.
We can assume that we have all the information for the xml document.

//Tony
 
M

Marc Gravell

Lots of ways... how do you currently have the data, and what is it?
That will determine the most suitable option.

Marc
 
J

Jon Skeet [C# MVP]

Lots of ways... how do you currently have the data, and what is it?
That will determine the most suitable option.

And also, which version of the framework are you using? LINQ to XML is
lovely, but only if you're using .NET 3.5...

Jon
 
M

Marc Gravell

And also, which version of the framework are you using? LINQ to XML is
lovely, but only if you're using .NET 3.5...

And where is the data coming from... for example, SQL/XML in SQL
Server 2005 / 2008 can work very well via "FOR XML PATH" in TSQL.

Marc
 
T

Tony Johansson

Hello!

Assume I have SQL server 2005 database where I want some data to be inserted
into a Xml document.


//Tony
 
M

Marc Gravell

Again, that doesn't give much away... if you are talking about small
changes to xml *already stored in an xml column*, then you can do this
at the server in TSQL (see link below) and avoid the IO cost of moving
the data. If you want to get data *out* of the SQL Server as xml, then
"FOR XML PATH" is your friend. Neither of these involve C#, so I won't
go into detail here...

If you are reading simple data from the server (perhaps via LINQ-to-
SQL) then you might want to do some work in C# - but as Jon correctly
observes, this depends on which framework you are using. With 2.0/3.0
you have XmlDocument and XmlWriter, but 3.5 introduces XDocument and
some language enhancements which make it trivial to construct xml in
various shapes.

Perhaps a simpified (but concrete) example would make things
clearer...? Can you explain more what you want to happen?

TSQL/XML link: http://msdn.microsoft.com/en-us/library/ms345117.aspx

Marc
 
T

Tony Johansson

Hello!

I just trying to learn some basic Xml so I have no specific task that I want
to accomplish.

//Tony
 
M

Marc Gravell

Then here's a very simple LINQ-to-XML example; anything more really
needs a specific question...

Marc

var data = new[] {
new {Name = "Fred", Age = 25},
new {Name = "Jo", Age = 11},
new {Name = "Sally", Age = 41}
};

var doc = new XDocument(
new XElement("xml",
new XAttribute("created", DateTime.Today),
from person in data
select new XElement("person",
new XAttribute("name", person.Name),
new XAttribute("age", person.Age))));

string xml = doc.ToString();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top