-7

I create a form so I use textbox and I would like when I push the button "send" he filled xml : for exemple 1 time :

<?xml version="1.0" encoding="utf-8"?>
<DonneesLocale>
  <Donnee>
    <id>1</id>
    <libelle>bla </libelle>
    <email_asso>bla@</email_asso>
    <login>bla</login>
    <psw>bla</psw>
    <site>bla</site>
    <description>bla</description>
    <data_1_lib></data_1_lib>
    <data_1_val></data_1_val>
    <data_2_lib></data_2_lib>
    <data_2_val></data_2_val>
  </Donnee>
</DonneesLocale>

and 2nd time when I push the button:

    <?xml version="1.0" encoding="utf-8"?>
<DonneesLocale>
  <Donnee>
    <id>1</id>
    <libelle>bla </libelle>
    <email_asso>bla@</email_asso>
    <login>bla</login>
    <psw>bla</psw>
    <site>bla</site>
    <description>bla</description>
    <data_1_lib></data_1_lib>
    <data_1_val></data_1_val>
    <data_2_lib></data_2_lib>
    <data_2_val></data_2_val>
  </Donnee>
<DonneesLocale>
  <Donnee>
    <id>2</id>
    <libelle>hello</libelle>
    <email_asso>hello@</email_asso>
    <login>hello</login>
    <psw>hello</psw>
    <site>hello</site>
    <description>hello</description>
    <data_1_lib></data_1_lib>
    <data_1_val></data_1_val>
    <data_2_lib></data_2_lib>
    <data_2_val></data_2_val>
  </Donnee>
</DonneesLocale>

Someone can help me please ? (Sorry for my English !) Thanks !

Snympi
  • 859
  • 13
  • 18
Victorya
  • 3
  • 1

3 Answers3

0

So if I get it correct, you want to append new data to your existing xml. For that you can chose to temporarily store the current xml and and add new data to it using Linq Xml.

To do this, now you need to modify your current code with an additional check for xml file exists before all calls to press button event. The code for appending to xml can be found Appending an existing XML file with XmlWriter

0

If you are using a list of objects then you can update the list on button click and parse your object list in xml like below:

var xml = new XElement("DonneesLocales", DonneesLocalList.Select(x => new XElement("DonneesLocale",
                   new XElement("Donnee",
                   new XElement("id"),
                   new XElement("libelle"),
                   new XElement("email_asso"),
                   new XElement("login"),
                   new XElement("psw"),
                   new XElement("site"),
                   new XElement("description"),
                   new XElement("data_1_lib"),
                   new XElement("data_1_val"),
                   new XElement("data_2_lib"),
                   new XElement("data_2_val")))));
kashi_rock
  • 539
  • 1
  • 5
  • 19
-1

Another option is their for XML Serialization and Deserialization which will be done with the help of XMLSerializer,

public class DonneesLocale
    {
        private List<Donnee> donnee = new List<Donnee>();

        [XmlArray("DonneesLocale")]
        public List<Donnee> Donnee
        {
            get { return donnee; }
            set { donnee = value; }
        }
    }

    [XmlType("Donnee")]
    public class Donnee
    {
        [XmlElement("id")]
        public int id { get; set; }

        [XmlElement("libelle")]
        public string libelle { get; set; }

        [XmlElement("email_asso")]
        public string email_asso { get; set; }

        [XmlElement("login")]
        public string login { get; set; }

        [XmlElement("psw")]
        public string psw { get; set; }

        [XmlElement("site")]
        public string site { get; set; }

        [XmlElement("description")]
        public string description { get; set; }

        [XmlElement("data_1_lib")]
        public string data_1_lib { get; set; }

        [XmlElement("data_1_val")]
        public string data_1_val { get; set; }

        [XmlElement("data_2_lib")]
        public string data_2_lib { get; set; }

        [XmlElement("data_2_val")]
        public string data_2_val { get; set; }
    }

DonneesLocale dnl = new DonneesLocale();

        private void Serialize(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                var temp = new Donnee() { id = i, libelle = "libelle " + i, email_asso = "email_asso " + i, login = "login " + i, psw = "psw " + i, site = "site " + i, description = "description " + i, data_1_lib = "data_1_lib " + i, data_1_val = "data_1_val " + i, data_2_lib = "data_2_lib " + i, data_2_val = "data_2_val " + i };
                dnl.Donnee.Add(temp);
            }

            try
            {
                // to Save columnorders to the file
                var serializer = new XmlSerializer(typeof(DonneesLocale));
                var ns = new XmlSerializerNamespaces();
                ns.Add("", "");


                using (TextWriter writer = new StreamWriter(@"Your XML Path"))
                {
                    serializer.Serialize(writer, dnl, ns);
                }

            }
            catch { }
        }

        private void Deserialize(object sender, EventArgs e)
        {
            try
            {
                if (File.Exists(@"Your XML Path"))
                {

                    var deserializer = new XmlSerializer(typeof(DonneesLocale));
                    using (TextReader reader = new StreamReader(@"Your XML Path"))
                    {
                        dnl = (DonneesLocale)deserializer.Deserialize(reader);
                    }

                }
            }
            catch
            {
            }
        }

all you need is to add an object to the list and serialize the object to XML whenever you want,

this will work as you expected,

Vicky S
  • 762
  • 4
  • 16