Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi am looking for add 1 text box and 3 comboboxs to an exisitng xml file using XDocument to be as result like the code below

XML
<pre><?xml version="1.0" encoding=""?>

<root>
++++++++++++++++
<Textbox>item1</Textbox>              
<combobox1>item2</combobox1>
<combobox2>item3</combobox2>
<combobox3>item4</combobox3>
++++++++++++++++
<table> 

  <rd>
    <id>45</id>
    <name>alex</name>
    <last>chali</last>
    <phone>33666998565</phone>
    <refF>
      <adresse>41301 US Hwy 280, Sylacauga AL 35150</adresse>
      <citie>NY</citie>
    </refF>
    <age>30</age>
    <mp>
      <degree>2</degree>
    </mp>
    <dpa>1</dpa>
  </rd>

  </table>

</root>


What I have tried:

string header = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Sheet1><table></table></Sheet1>";
XDocument doc = XDocument.Parse(header);
XElement table = doc.Root.Element("table");

foreach (DataRow row in dt.AsEnumerable())
{
    XElement rd = new XElement("rd", new object[] {
        new XElement("id", row["id"]),
        new XElement("name", row["name"]),
        new XElement("last", row["last"]),
        new XElement("phone", row["phone"]),
        new XElement("refF", new object[] {
            new XElement("adresse", row["adresse"]),
            new XElement("citie", row["citie"]),
        }),
        new XElement("age", row["age"]),
        new XElement("mp", new XElement("degree", row["mp"])),
        new XElement("dpa", row["dpa"])
    });

    table.Add(rd);
}
doc.Save(FILENAME);
Posted
Updated 13-May-20 6:41am

1 solution

Simple:
C#
XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", null),
    new XElement("root",
        new XElement("Textbox", YOUR_TEXTBOX_VALUE_HERE),
        new XElement("combobox1", YOUR_COMBOBOX1_VALUE_HERE),
        new XElement("combobox2", YOUR_COMBOBOX2_VALUE_HERE),
        new XElement("combobox3", YOUR_COMBOBOX3_VALUE_HERE),
        new XElement("table",
            dt.AsEnumerble().Select(row => new XElement("rd",
                new XElement("id", row["id"]),
                new XElement("name", row["name"]),
                new XElement("last", row["last"]),
                new XElement("phone", row["phone"]),
                new XElement("refF",
                    new XElement("adresse", row["adresse"]),
                    new XElement("citie", row["citie"]),
                ),
                new XElement("age", row["age"]),
                new XElement("mp", new XElement("degree", row["mp"])),
                new XElement("dpa", row["dpa"])
            ))
        )
    )
);

doc.Save(FILENAME);
 
Share this answer
 
Comments
Usarsef 13-May-20 13:51pm    
Now when i did this the value from the textbox and combobox doesn't appear in xml file
Richard Deeming 13-May-20 13:51pm    
Then you've done something wrong.
Usarsef 14-May-20 13:10pm    
I fixed the problem thanks for help.
Maciej Los 14-May-20 2:10am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900