简体   繁体   中英

Adding elements to XDocument at runtime from DataTable

I am trying to generate an XML using XDocument by pulling data from a DataTable at runtime. I want to have the output in this format:

<Document>
  <Alphabets>
    <Data>
      <Capital>AAA</Capital>
      <Small>aaa</Small>
    </Data>    
  </Alphabets>
  <Language>
      <Name>English</Name>
  </Language>
  <Alphabets>
    <Data>
      <Capital>BBB</Capital>
      <Small>bbb</Small>
    </Data>
  </Alphabets>
  <Language>
      <Name>English</Name>
  </Language>
</Document>

The Language element has to be present after every Alphabets element. I have tried very hard to achieve this but I am unable to put this Alphabets tag after every Language element. What I have achieved is this, where the Language element is falling inside Alphabets element:

<Document>
  <Alphabets>
    <Data>
      <Capital>AAA</Capital>
      <Small>aaa</Small>
    </Data>
    <Language>
      <Name>English</Name>
    </Language>
  </Alphabets>
  <Alphabets>
    <Data>
      <Capital>BBB</Capital>
      <Small>bbb</Small>
    </Data>
    <Language>
      <Name>English</Name>
    </Language>
  </Alphabets>
</Document>  

Here is my code :

        static void Main(string[] args)
        {
            DataTable dtAlpha = new DataTable("Alphabetss");
            dtAlpha.Columns.Add("Capital", typeof(string));
            dtAlpha.Columns.Add("Small", typeof(string));

            dtAlpha.Rows.Add("AAA", "aaa");
            dtAlpha.Rows.Add("BBB", "bbb");

            XDocument doc = new XDocument(
                new XDeclaration("1.0", "UTF-8", null),
                new XElement("Document",
                        from row in dtAlpha.AsEnumerable()
                        select new XElement("Alphabets",
                            new XElement("Data",
                                new XElement("Capital", row.Field<string>("Capital")),
                                new XElement("Small", row.Field<string>("Small"))
                                         ),
                        new XElement("Language",
                            new XElement("Name", "English")
                            )))
                            );
            Console.WriteLine(doc.ToString());
            Console.ReadKey();
        }  

Please help me on this.

You can try this way :

XDocument doc = new XDocument(
    new XDeclaration("1.0", "UTF-8", null),
    new XElement("Document")
);

foreach(var row in dtAlpha.AsEnumerable())
{
    var alphabets = new XElement("Alphabets",
                        new XElement("Data",
                            new XElement("Capital", row.Field<string>("Capital")),
                            new XElement("Small", row.Field<string>("Small"))
                        )
                    );
    var language = new XElement("Language",
                        new XElement("Name", "English")
                    );
    doc.Root.Add(alphabets);
    doc.Root.Add(language);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM