Dominating
Dominating

Reputation: 2930

How to create this kind of XML?

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

Answers (3)

Jon Skeet
Jon Skeet

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

dtb
dtb

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

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174289

Have a look at XDocument. There is a nice example.

Upvotes: 3

Related Questions