webForumDet fria alternativet

[C#]Indentering av xml?

0 svar · 353 visningar · startad av pirate

pirateMedlem sedan aug. 200541 inlägg
#1

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);
            }
        }
133 ms totalt · 3 externa anrop · v20260731065814-full.fb544a5a
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
129 ms — hämta tråd, inlägg och bilagor (db)