samedi 25 avril 2015

How can I write to the root element?


When I use the following code I can write successfully to the XML file however it will write outside the root element.

        StreamWriter sw = File.AppendText(Environment.CurrentDirectory + "\\Settings.xml");
        XmlTextWriter xtw = new XmlTextWriter(sw);

        xtw.WriteStartElement("connection");
        xtw.WriteElementString("id", name);
        xtw.WriteElementString("Version", "2.3.1");
        xtw.WriteElementString("Server", ip_textBox.Text);
        xtw.WriteElementString("Port", port_textBox.Text);
        xtw.WriteElementString("Uid", user_textBox.Text);
        xtw.WriteElementString("Password", pass_textBox.Text);

        xtw.Close();

After the code runs the XML looks like this:

<?xml version='1.0' encoding='utf-8' ?>
<root>

</root>
<connection><id>test</id><Version>2.3.1</Version><Server>127.0.0.1</Server><Port>3306</Port><Uid>root</Uid><Password>root</Password>

When it should look like:

<?xml version='1.0' encoding='utf-8' ?>
<root>
  <connection>
     <id>Test</id>
     <Version>2.3.1</Version>
     <Server>127.0.0.1</Server>
     <Port>3306</Port>
     <Uid>root</Uid>
     <Password>root</Password>
  </connection>
</root>

Again my question is how can I write inside the root element?


Aucun commentaire:

Enregistrer un commentaire