Reputation: 153
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);
Upvotes: 1
Views: 194
Reputation: 34429
Here is class using XML Linq :
public class Simulator
{
XDocument doc = null;
public Simulator(string filename)
{
doc = XDocument.Load(filename);
}
public Simulator(string id, string description)
{
string ident = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Simulator></Simulator>";
doc = XDocument.Parse(ident);
XElement flight = new XElement("Flight", new object[] {
new XAttribute("ID", id),
new XAttribute("Description", description)
});
doc.Root.Add(flight);
}
public string AddUser(string flightID, string role, string number, string name, string surname, string rank)
{
XElement flight = doc.Descendants("Flight").Where(x => (string)x.Attribute("ID") == flightID).FirstOrDefault();
if (flight != null)
{
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);
return "GOOD";
}
else
{
return "Flight ID doesn't exist";
}
}
public string AddFlight(string flightID, string description)
{
XElement flight = doc.Descendants("Flight").Where(x => (string)x.Attribute("ID") == flightID).FirstOrDefault();
if (flight == null)
{
flight = new XElement("Flight", new object[] {
new XAttribute("ID", flightID),
new XAttribute("Description", description)
});
doc.Root.Add(flight);
return "Good";
}
else
{
return "Flight ID already exists";
}
}
public string AddErrors(string flightID, XElement errorLog)
{
XElement flight = doc.Descendants("Flight").Where(x => (string)x.Attribute("ID") == flightID).FirstOrDefault();
if(flight == null)
{
return "Flight ID doesn't exist";
}
else
{
flight.Add(errorLog);
return "GOOD";
}
}
public void WriteXml(string filename)
{
doc.Save(filename);
}
}
Upvotes: 1
Reputation: 36541
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.
Upvotes: 0