开发者

How to create this kind of XML?

I want to create via C# code XML like this:

<Title>
   <A>
       <aaaaaaaaaaaaa/>
   </A>
   <B>
开发者_如何学JAVA       <bbbbbbbbbbbbb/>
   </B>
</Title>

With what code should i create tree like thist?


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.


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


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")));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜