---
title: "[C#]Indentering av xml?"
type: "forum-thread"
url: "https://www.webforum.nu/amne/dotnet/175142-c-indentering-av-xml"
topic: ".NET"
topic_url: "https://www.webforum.nu/amne/dotnet"
author: "pirate"
published: "2008-11-03T19:34:49.000Z"
updated: "2008-11-03T19:34:49.000Z"
replies: 0
views: 379
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/dotnet/175142-c-indentering-av-xml"
---

# [C#]Indentering av xml?

## #1 — pirate, 2008-11-03T19:34Z

Har lite problem med strukturering av kod vilket leder till att jag inte får till indentering av xml   :( 
Hade uppskattat lite hjälp

```php
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);
            }
        }
```

Permalänk: https://www.webforum.nu/p/175142

---

Tråden på webben: https://www.webforum.nu/amne/dotnet/175142-c-indentering-av-xml
