0

I need to write XML files with a tilde as a separator between portions, like so:

....
<Company>
<CompanyDetail>Blah</CompanyDetail>
<Phone>0000000000</Phone>
</Company>
~
<Company>
<CompanyDetail>Blah</CompanyDetail>
<Phone>0000000000</Phone>
</Company>
....

The way to do that normally in C# would be along the lines of

writer = new XmlTextWriter(fileName, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();

int remainingCompanies = companyList.Count;

foreach (Company company in companyList)
{
    writer.WriteStartElement("Company");
    writer.WriteStartElement("CompanyDetail");
    writer.WriteString("company.companyDetail.toString()");
    writer.WriteEndElement();
    writer.WriteStartElement("Phone");
    writer.WriteString("company.phone.toString()");
    writer.WriteEndElement();
    writer.WriteEndElement();

    if (remainingCompanies-- > 1)
    {
        writer.WriteString(\n~\n);
    }
}

But whenever I do this, the resulting XML file ends up being poorly formatted like so:

<Company>
<CompanyDetail>FirstCompany</CompanyDetail>
<Phone>1111111111</Phone>
</Company>
~
<Company><CompanyDetail>SecondCompany</CompanyDetail><Phone>2222222222</Phone></Company>
~
<Company><CompanyDetail>ThirdCompany</CompanyDetail><Phone>3333333333</Phone></Company>
....

When there's much more information for each company than just CompanyDetail and Phone, you can imagine how difficult it gets to look through the single line to visually find what you need in the XML.

My current workaround is to replace the tilde with a comment, but how do I have a tilde separating parts of this XML file AND maintain clean formatting?

  • 2
    Why do you need to brake your xml file? Output will not be valid xml – Sergey Berezovskiy Jun 19 '17 at 16:03
  • 1
    https://stackoverflow.com/questions/7535280/writing-formatted-xml-with-xmlwriter – Paul Abbott Jun 19 '17 at 16:04
  • @CharlesMager True, my bad. However, using a separator in XML like that doesn't seem like a great idea. If you want to structure your XML file, XML is built to do that already. But the decision may be out of OP's control. – 15ee8f99-57ff-4f92-890c-b56153 Jun 19 '17 at 16:10
  • 3
    The issue here is one of significant whitespace. Once you add text inside your root element, it becomes 'mixed content' and all whitespace is now important. As a result, the indentation behaviour is stopped. This is explained in the [remarks for `XmlWriterSettings`](https://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.indent(v=vs.110).aspx). – Charles Mager Jun 19 '17 at 16:11
  • 1
    @SergeyBerezovskiy prior conventions with the software I'm working with. It is odd, I know, but it worked well before. – A. Tarasenko Jun 19 '17 at 16:11
  • Instead of using a tilde use an actual xml comment line. Don't corrupt the xml with a tilde. – jdweng Jun 19 '17 at 16:45
  • 1
    @jdweng this isn't corruption, it's well formed xml. It's odd, granted. But it's permitted. – Charles Mager Jun 19 '17 at 17:14

0 Answers0