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);
check for id part
var userElements = xDoc.Descendants("Flight").ToList();
string lastID = userElements.Select(x => (x.Attribute("ID").Value))
.OrderByDescending(x => x)
.First()
.ToString();
int lastID_int = Int16.Parse(lastID) + 1;
if (lastID_int <= 0)
lastID_int = 0;
string current = lastID_int.ToString();
Simulator sim = new Simulator(current, textbox_description.Text);