Home > Blockchain >  Saving the values entered in the textbox to the xml file
Saving the values entered in the textbox to the xml file

Time:09-01

I am new to XML and I tried to create a window form that collects num, names, surnames, and ranks for each instructor, student, operator, and technician. But this code is going to be so long. Do you have any suggestions to create this XML on c#? Info will be taken from 4 text boxes for each employee's num, name, surname, and rank. also, the description will be taken from the textbox.

<Simulator>
    <Flight ID="1" Description="rainy_day">
      <Instructor Num="14213124">
        <Name>Jack</Name>
        <Surname>Sparrow</Surname>
        <Rank>General</Rank>
      </Instructor>
      <Student Num="56475679">
        <Name>Adam</Name>
        <Surname>Black</Surname>
        <Rank>Lieutenant</Rank>
      </Student>
      <Operator Num="75846474">
        <Name>Gabriel</Name>
        <Surname>Red</Surname>
        <Rank>Op</Rank>
      </Operator>
      <Technician Num="85425484">
        <Name>Samuel</Name>
        <Surname>Long</Surname>
        <Rank>Tec</Rank>
      </Technician>
    </Flight>
</Simulator>

this is the code i tried. It's gonna be so long if i write this again and again for each employee

XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                XmlElement rootNode = xmlDoc.DocumentElement;
                xmlDoc.InsertAfter(xmlDeclaration, rootNode);

                XmlNode parentNode = xmlDoc.CreateElement("Users");
                XmlNode subNode = xmlDoc.CreateElement("Instructor");

                XmlAttribute numAtt = xmlDoc.CreateAttribute("Num");
                numAtt.Value = num.ToString();
                XmlAttribute nameAtt = xmlDoc.CreateAttribute("Name");
                nameAtt.Value = name;
                XmlAttribute surnameAtt = xmlDoc.CreateAttribute("Surname");
                surnameAtt.Value = surname;
                XmlAttribute rankAtt = xmlDoc.CreateAttribute("Rank");
                rankAtt.Value = rank;

                subNode.Attributes.Append(numAtt);
                subNode.Attributes.Append(nameAtt);
                subNode.Attributes.Append(surnameAtt);
                subNode.Attributes.Append(rankAtt);

                xmlDoc.AppendChild(parentNode);
                parentNode.AppendChild(subNode);

CodePudding user response:

You should probably use the xmlSerializer instead of trying to manually create the xml tree.

This should let you create classes for your data and let the serializer worry about transforming the objects to xml:

public class Person{
    public string Num{get;set;}
    public string Name {get;set;}
    public string Surname {get;set;}
    public string Rank{get;set;}
}
public class Flight{
    public Person Instructor {get;set;}
    public Person Student {get;set;}
    ....
}

var serializer = new XmlSerializer(typeof(Flight));
var writer = new StreamWriter(filename);
serializer.Serialize(writer, myFlight);

This might not give you exactly the layout you are after, but that can often be controlled by attributes. See also Json .net for the more popular alternative to xml.

CodePudding user response:

Here is class using XML Linq :

   public class Simulator
    {
        XDocument doc = null;
        XElement flight = null;
        public Simulator(string id, string description)
        {
            string ident = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Simulator></Simulator>";
            doc = XDocument.Parse(ident);
            flight = new XElement("Flight", new object[] {
                new XAttribute("ID", id),
                new XAttribute("Description", description)
            });

            doc.Root.Add(flight);
        }
        public void AddUser(string role, string number, string name, string surname, string rank)
        {
            XElement user = new XElement(role, new object[] {
                new XAttribute("Num", number),
                new XElement("Name", name),
                new XElement ("Surname", surname),
                new XElement("Rank", rank)
            });

            flight.Add(user);

        }

        public void WriteXml(string filename)
        {
            doc.Save(filename);
        }
    }
  • Related