开发者

how to create N Level xml file

I need to create xml file that have N Level.

for ex. in my below example I have 'AlbumDetails' is root element and 'PrintPackage' is another child root and 'UpgradePackage' is another child root.

Can any one let me know how can i make N Level/Multi Level XML in c#.

<AlbumDetails>
  <Album Id="203">
    <Institute>Oxford</Institute>
    <Venue>Wallingford School</Venue>
    <PrintPackage>
      <SizeName>Combination Pack</SizeName>
      <Price>1.00</Price>
      <Weight>60.00</Weight>
      <UpgradePackage>
      <SizeName>Upgrade 1</SizeName>
开发者_如何学JAVA      <Price>1.00</Price>
      <Weight>60.00</Weight>
      </UpgradePackage>
      <SizeName>Standard Pack</SizeName>
      <Price>90.0000</Price>
      <Weight>600.0000</Weight>
    </PrintPackage>
  </Album>
</AlbumDetails>


You are looking for the XmlWriter class.

Update: In case you want to create a document similar to the one above:

var builder = new StringBuilder();
using (var writer = XmlWriter.Create(builder))
{
    writer.WriteStartElement("AlbumDetails");
    writer.WriteStartElement("Album");
    writer.WriteAttributeString("Id", "203");

    writer.WriteElementString("Venue", "Wallingford School");

    writer.WriteStartElement("PrintPackage");

    .... etc.

    writer.WriteEndElement(); // close PrintPackage

    writer.WriteEndElement(); // close Album
    writer.WriteEndElement(); // close AlbumDetails
}
Console.WriteLine(builder.ToString());


Use XDocument and XElement from System.Xml.Linq ( Linq2Xml )

XDocument doc = new XDocument(new XDeclaration("1.0","utf-8","true"),
            new XElement("AlbumDetails",
                new XElement("Album",new XAttribute("Id","203"),
                    new XElement("Institute","Oxford"),
                    new XElement("Venue","Wallingford School")
                    ...

                    )
                )
            );

If you are just looking for XElement only, you can build it up in a similar way. You can have a processingElement and create the XElement based on your logic and do

doc.Add(processingElement);

or

ele.Add(processingElement);


They're not really "child roots" - they're just elements which have other child elements.

Personally I'd use LINQ to XML. It's by far the simplest XML API I've used. For example:

var element = new XElement("AlbumDetails",
    new XElement("Album",
        new XAttribute("ID", 203"),
        new XElement("Institute", "Oxford"),
        new XElement("Venue", "Wallingford School"),
        new XElement("PrintPackage",
            new XElement("SizeName", "Combination Pack"),
            // etc
            new XElement("UpgradePackage",
                new XElement("SizeName", "Upgrade 1"),
                // etc
            )
        )
    );

Of course, you don't have to build up everything in a single statement - you can add child nodes separately, potentially constructing them entirely separately. Indeed, you may want a separate method to create each "container" element.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜