Har lite problem med strukturering av kod vilket leder till att jag inte får till indentering av xml :(
Hade uppskattat lite hjälp
public void writeToXmlFile()
{
try
{
//creating a test file
string filename = "XML" + DateTime.Now.Day + ".xml";
//Create a new xml document to write to
XmlDocument xmlDoc = new XmlDocument();
//Deleting existing file to not append at the
//end of file
File.Delete(filename);
try
{
xmlDoc.Load(filename);
}
catch (FileNotFoundException)
{
try
{
//if file is not found, create a new xml file
XmlTextWriter xmlWriter = new XmlTextWriter(filename, Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 4;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("virtual");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
xmlWriter.Close();
//xmlDoc.Load(filename);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
xmlDoc.Load(filename);
XmlNode root = xmlDoc.DocumentElement;
foreach (EcuID eID in _virtEcuIdTable)
{
XmlElement childNode1 = xmlDoc.CreateElement("ecu");
childNode1.SetAttribute("id", eID.ecu_id);
root.AppendChild(childNode1 as XmlNode);
foreach (Request rq in eID.ecuIdTable)
{
XmlElement childNode2 = xmlDoc.CreateElement("request");
childNode2.SetAttribute("data", rq.reqData);
childNode1.AppendChild(childNode2);
foreach (Response resp in rq.reqDataTable)
{
XmlElement childNode3 = xmlDoc.CreateElement("response");
childNode3.SetAttribute("time", resp.time);
childNode3.SetAttribute("data", resp.data);
childNode2.AppendChild(childNode3);
}
}
}
xmlDoc.Save(filename);
//Loads the changes into the textbox
parent.openFile(filename);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}