Note that there are some explanatory texts on larger screens.

plurals
  1. POIndentation in programmatically created XML in C#
    primarykey
    data
    text
    <p>I have an XmlWriter as follows</p> <pre><code>XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineHandling = NewLineHandling.Entitize; using (XmlWriter currentWriter = XmlWriter.Create(filePath, settings)) { workingXmlDocument.WriteTo(currentWriter); currentWriter.Flush(); } // using (XmlWriter xw = XmlWriter.Create(FilePath)) </code></pre> <p>I have programmatically created an Xml Document that I am trying to write with this, and it is writting just fine, except that I have a block of elements that are created in a loop and it is putting all these elements onto one line. If you need to see any other code from the program let me know and I can put it here. I welcome any ideas even if they aren't full solutions.</p> <p><strong>Update - the complete method where the problem is happening</strong></p> <pre><code>private void XMLReaderUtil(string filePath) { XDocument workingXmlDocument; using (XmlReader currentReader = XmlReader.Create(filePath)) { workingXmlDocument = XDocument.Load(currentReader); foreach (KeyValuePair&lt;string, string&gt; currentIteration in logDataRaw) { FileInfo currentEntry = new FileInfo(currentIteration.Value); string testString = currentEntry.Directory.Name; if (testString.Contains(" ")) { testString = testString.Replace(" ", "_"); } IEnumerable&lt;XElement&gt; list = from XElement e in workingXmlDocument.Descendants(wixNS + testString) select e; DirectoryInfo temp = currentEntry.Directory; while (list.Count() == 0 &amp;&amp; !temp.Name.Contains(":")) { testString = temp.Name; if (testString.Contains(" ")) { testString = testString.Replace(" ", "_"); } list = from XElement e in workingXmlDocument.Descendants(wixNS + testString) select e; temp = temp.Parent; } // while (list.Count() == 0 &amp;&amp; !temp.Name.Contains(":")) if (list.Count() == 0) { IEnumerable&lt;XElement&gt; targetDirectory = from XElement e in workingXmlDocument.Descendants(wixNS + "Directory") where e.Attribute("Id").Value == "TARGETDIR" select e; var outXml = new XElement("root"); foreach (var item in targetDirectory) { item.Add(new XElement( "Directory", new XAttribute("new", 1))); outXml.Add(item); } outXml.Save(@"c:\users\adkins\desktop\temp.wxs", SaveOptions.None); } } // foreach (KeyValuePair&lt;string, string&gt; kvp in xmlDataChanged) // return workingXmlDocument; } // using (XmlReader sr = XmlReader.Create(FilePath)) IEnumerable&lt;XElement&gt; cleanup = from XElement e in workingXmlDocument.Descendants(wixNS + "Directory") select e; Collection&lt;string&gt; keep = new Collection&lt;string&gt;(); XElement[] blah; blah = cleanup.ToArray(); for (int i = 0; i &lt; blah.Length; i++) { if (!keep.Contains(blah[i].Attribute("Id").Value)) { keep.Add(blah[i].Attribute("Id").Value); } else { blah[i].Remove(); } } workingXmlDocument.Save(filePath, SaveOptions.None); // XMLWriter attempt //XmlWriterSettings settings = new XmlWriterSettings(); //settings.Indent = true; //settings.IndentChars = "\t"; //settings.NewLineHandling = NewLineHandling.Entitize; //settings.NewLineChars = "\n"; //settings.CloseOutput = true; //settings.Encoding = System.Text.Encoding.UTF8; //using (XmlWriter currentWriter = XmlWriter.Create(filePath, settings)) //{ // workingXmlDocument.WriteTo(currentWriter); // currentWriter.Flush(); //} // using (XmlWriter xw = XmlWriter.Create(FilePath)) //TextWriter test run - nothing changed //XmlTextWriter writer = new XmlTextWriter(filePath, System.Text.Encoding.UTF8); //writer.Formatting = Formatting.Indented; //writer.IndentChar = '\t'; //writer.Indentation = 1; //workingXmlDocument.WriteTo(writer); //writer.Flush(); //writer.Close(); } </code></pre> <p>Here is the complete problem method. It includes my attempts at various forms of saving the file. It is a complete mess due to my efforts to solve this problem and problems that I encountered before getting to this one. Any help would be greatly appreciated!</p> <p>The variable "logDataRaw" is a dictionary containing the name of a file and the path to where it is stored. If you need any other clarification please let me know.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload