Reputation: 2930
I want to create via C# code XML like this:
<Title>
<A>
<aaaaaaaaaaaaa/>
</A>
<B>
<bbbbbbbbbbbbb/>
</B>
</Title>
With what code should i create tree like thist?
Upvotes: 0
Views: 91
Reputation: 1499800
Well, if you can use LINQ to XML it's insanely easy:
XElement root = new XElement(
"Title",
new XElement("A",
new XElement("aaaaaaaaaaaaa")),
new XElement("B",
new XElement("bbbbbbbbbbbbb"))
);
Additionally, if you need to build this dynamically from data (which you probably will), you can include the queries within the constructor calls, and it will all work.
LINQ to XML really is an impressively easy-to-use API. Of course, it does require .NET 3.5 or higher.
Upvotes: 4
Reputation: 217233
Have a look at LINQ to XML, i.e. the System.Xml.Linq namespace:
var result = new XElement("Title",
new XElement("A",
new XElement("aaaaaaaaaaaaa")),
new XElement("B",
new XElement("bbbbbbbbbbbbb")));
Upvotes: 2