0

what I want is like this:

  <a>
    <b>
      <c>
        value
      </c>
    </b>
  </a>

I've tried: WriteValue()/WriteString() will not start a new line: "<c>value</c>" WriteWhitespace(Environment.NewLine) will not keep the indent:

  <a>
    <b>
      <c>
  value
  </c>
    </b>
  </a>

For some reason I have to use XmlTextWriter.

  • Try the properties, writer.Formatting = Formatting.Indented; writer.Indentation = 4; – jac Sep 04 '14 at 02:47
  • you may misunderstood, I've already use formatting, so the a-b-c will keep indent, but when I want add a value in a new line, the value cannot. – Crazyisgift Sep 04 '14 at 02:55

2 Answers2

1

You have to use XmlWriterSettings. see the code sample

XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.IndentChars = "\t";
        XmlWriter writer = XmlWriter.Create(@"sample.xml", settings);
        writer.WriteStartElement("a");
        writer.WriteStartElement("b");
        writer.WriteStartElement("c");
        writer.WriteValue("value");
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.Close();
Seminda
  • 1,745
  • 12
  • 15
0

You can implement a small custom writer to accomplish that. See my answer here: https://stackoverflow.com/a/33542165/5114784

Community
  • 1
  • 1
György Kőszeg
  • 17,093
  • 6
  • 37
  • 65