0

i have this simple code:

var xel=new XElement("myElement");
xel.Value="\r"; // assign carriage return
xel.Save(@"file.xml",,SaveOptions.DisableFormatting)

If i open the file with notepad++ and i show all chars, i see that the element value is saved as CR+LF.

Then, i'm not able to read the file again.

Anyone can help me? Thanks!

Edit: i found follow solution, but i' m not sure that it is the best one.

   var xel = new XElement("myElement");
    xel.Value = "\r";

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.NewLineHandling=NewLineHandling.None;
    settings.OmitXmlDeclaration = true;
    System.Text.StringBuilder result = new System.Text.StringBuilder();
    using (XmlWriter writer = XmlWriter.Create(result, settings)) {
        xel.WriteTo(writer);
    }
    var text = result.ToString(); // expected output
stefano m
  • 4,094
  • 5
  • 28
  • 27

1 Answers1

0

this should work (changed SaveOptions ) refer this

 var xel=new XElement("myElement");
 xel.Value="\r"; // assign carriage return
 xel.Save(@"C:\file.xml",SaveOptions.None);
faby
  • 7,394
  • 3
  • 27
  • 44