简体   繁体   中英

Indentation in XML files when loading and saving

I know this subject has been out before but I can't seem to figure out how to do it even with all the previous post about it.

So in my code I'm creating and filling an xml file with some stuff. The code comes back to the xml code ofter the file has been created and closed, to write some more. The problem is that no matter what combination of stream/xml readers/writers I use, the indentation gets weird some where in the text.

Here is the code:

XmlDocument xml = new XmlDocument();
xml.PreserveWhitespace = true;
XmlElement root = null;

FileStream fs = null;

if (File.Exists(fileName))
{
    fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);

    xml.Load(fs);
    if (xml.GetElementsByTagName("anndata").Count > 0)
         root = (XmlElement)xml.GetElementsByTagName("anndata").Item(0);
}
else
{
    fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.ReadWrite);
    root = xml.CreateElement("anndata");
    xml.AppendChild(root);
}

    //some writing to the xml doc.

fs.SetLength(0);
xml.Save(fs);
fs.Close();

Right now everything comes on one line in the xml file and I would love some indentation, so any tips?

Edit:

I changed the last bit to this:

fs.SetLength(0);
XmlTextWriter writer = new XmlTextWriter(fs, Encoding.Unicode);
writer.Formatting = Formatting.Indented;
xml.Save(writer);
writer.Close();
fs.Close();

but now the result after 1 creation of the file and two more writings to it, the file looks likes like this:

<?xml version="1.0" encoding="utf-16"?>


<anndata>
  <ann name="test1">
    <weights>
      <weight node1="7" node2="8" weight="-0.5699342" />
      <weight node1="3" node2="8" weight="-0.2840658" />
      <weight node1="0" node2="10" weight="-0.03812337" />
      <weight node1="7" node2="10" weight="0.2714444" />
      <weight node1="3" node2="10" weight="-0.2163633" />
      <weight node1="0" node2="2" weight="0.8593098" />
      <weight node1="7" node2="2" weight="-0.5274166" />
      <weight node1="3" node2="2" weight="-0.1043958" />
      <weight node1="8" node2="1" weight="0.5244942" />
      <weight node1="10" node2="1" weight="0.7568867" />
      <weight node1="2" node2="1" weight="0.7543423" />
      <weight node1="8" node2="5" weight="-0.1524385" />
      <weight node1="10" node2="5" weight="-0.09445882" />
      <weight node1="2" node2="5" weight="-0.2441895" />
      <weight node1="8" node2="11" weight="0.5473043" />
      <weight node1="10" node2="11" weight="-0.1439976" />
      <weight node1="2" node2="11" weight="0.1768849" />
      <weight node1="0" node2="1" weight="-0.4702233" />
    </weights>
  </ann>
<ann name="test1"><weights><weight node1="7" node2="8" weight="-0.5699342" /><weight node1="3" node2="8" weight="-0.2840658" /><weight nod.... //no new lines here.

Edit2:

If a remove the "fs.SetLength(0);" it works better, but now the <anndata> root element is duplicated the second time I write to it and the XmlDocument trows an exception on that the third time I load it for a third writing.

The way you are trying to do it using an xml document looks fine. Try reading the doc from your file (and closing the file) and then saving the doc back to the file as a separate step (rather than using a read/write file and trying to truncate it between the load and save steps)

Beyond that, just check that you have set the correct formatting properties in the doc before you ask it to save.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM