0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Mockarutan
  • 442
  • 4
  • 28
  • Check good answers here http://stackoverflow.com/questions/2374654/how-do-you-create-an-indented-xml-string-from-an-xdocument-in-c – Ilya Ivanov Feb 20 '13 at 16:54
  • The problem with that is that I'm terrible with all the readers everywhere. So I'm not sure how to make the code able to create a new file and write to it with indentation and open up a file and writing some more with right indentation. The Filestream thing i use now is the only one I know how to append and create files with. And FileStream don't seem to work well XmlTextWriter. – Mockarutan Feb 20 '13 at 16:59
  • try to consider answer below and reply if you encounter any problems – Ilya Ivanov Feb 20 '13 at 17:00
  • It did not work, look at my two edits. – Mockarutan Feb 20 '13 at 17:03

1 Answers1

0

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.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137