Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode multiline strings as multiline strings. #41

Open
Pxtl opened this issue Oct 17, 2016 · 2 comments
Open

Encode multiline strings as multiline strings. #41

Pxtl opened this issue Oct 17, 2016 · 2 comments

Comments

@Pxtl
Copy link
Contributor

Pxtl commented Oct 17, 2016

The raw xml is difficult to read with multline strings, as the strings get encoded as hideous xml escapes.

Use "OmitXmlDeclaration = true," in XmlWriterSettings to correct.

Unit test demonstrating loopback in this case:

var teststr = @"
this
<is>
  a

    test
    .
";
var ms = new MemoryStream();
var writer = XmlWriter.Create(ms, new XmlWriterSettings()
{
    NewLineOnAttributes = true,
    Indent = true,
    IndentChars = "  ",
    NewLineChars = Environment.NewLine,
    OmitXmlDeclaration = true,
    NewLineHandling = NewLineHandling.None
});
writer.WriteStartElement("root");
writer.WriteAttributeString("multilineTest", teststr);
writer.WriteEndElement();
writer.Flush();
var asStr = System.Text.Encoding.UTF8.GetString(ms.GetBuffer());
asStr.Dump();

XmlReader reader = new XmlTextReader(new MemoryStream(Encoding.UTF8.GetBytes(asStr)));
reader.Read();
Assert.AreEqual(reader.GetAttribute("multilineTest"), teststr);
@mirhagk
Copy link
Owner

mirhagk commented Oct 17, 2016

It looks like the NewLineHandling = NewLineHandline.None is actually the relevant part. Doing OmitXmlDeclaration = false seems to output the multilineTest attribute identically (just make sure to do 2 more reads to skip past the declaration and the whitespace).

The NewLineHandling seems to roundtrip fine, so I'm totally fine with adding that (definitely will make it easier to deal with multi line strings) and if for some reason in the future there's issues caused by it (some naive parser minifying it) we can add an option to turn this back on.

@Pxtl
Copy link
Contributor Author

Pxtl commented Oct 18, 2016

Blarg, yes, I copypasta'd the wrong line. Yes, NewLineHandline.None.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants