-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
XElement formating issue #29260
Comments
@VBAndCs, XML spec allows to normalize whitespace around the text (I believe it should not touch whitespaces within text but not 100% sure without looking at the spec). The situation should improve if you put XML parser is still allowed to normalize line endings regardless of that setting. Your best bet is to use XmlTextReader/XmlTextWriter with WhitespaceHandling option - it should work best when whitespaces are important (you shouldn't need to use Please let me know if this fixes your issue. |
@krwq <cshtml xml:space="preserve">
<p>Test</p>
@foreach (var i in items)
{
<h>item: </h><div xml:space="preserve"><p>i</p></div>
}
<p>End Test</p></cshtml> I tried XmlTextReader/XmlTextWriter before and got the same results. The only thing worked for me is this workaround: <cshtml>
<p>Test</p>
<zmiItem0/>
<zmlitem1/>
<h>item: </h>
<div>
<p>i</p>
</div>
<zmlitem2/>
<p>End Test</p>
</cshtml> Then I replace |
@VBAndCs could you paste the code you are using for reading and writing XML with XmlTextReader/Writer? I wanted to double check if there is anything else we can do to improve this. One other thing you might want to check: writer might be writing new lines but |
@krwq |
@VBAndCs we will need a complete code sample which is not working correctly for this to be actionable.. Note XML is one of the largest libraries in corefx and there are multiple different Readers/Writers. |
I already gave the sample using VB.NET xml literals. You can try it as is: Dim cshtml =
<cshtml>
<p>Test</p>
@foreach (var i in items)
{
<h>item: </h>
<div>
<p>i</p>
</div>
}
<p>End Test</p>
</cshtml>
Dim S = cshtml.ToString()
Console.WriteLine(S) You can trace the XElemnt.ToString() and see why it produces that code. var cshtml =
@"<cshtml>
<p>Test</p>
@foreach (var i in items)
{
<h>item: </h>
<div>
<p>i</p>
</div>
}
<p>End Test</p>
</cshtml>";
var x = System.Xml.Linq.XElement.Parse(cshtml);
var S = cshtml.ToString(); You can trace Parse and ToString. |
@VBAndCs running C# above here is the output I get + Console.WriteLine: <cshtml>
<p>Test</p>
@foreach (var i in items)
{
<h>item: </h>
<div>
<p>i</p>
</div>
}
<p>End Test</p>
</cshtml> this looks the same as an input to me... |
Sorry, it exactly prints the input itself :D |
var cshtml =
@"<cshtml>
<p>Test</p>
@foreach (var i in items)
{
<h>item: </h>
<div>
<p>i</p>
</div>
}
<p>End Test</p>
</cshtml>";
var xml = System.Xml.Linq.XElement.Parse(cshtml);
var s = xml.ToString();
Console.WriteLine(s); |
@VBAndCs didn't notice that, just quickly copy pasted... 🤦♂️ |
@VBAndCs try this out: <cshtml>
<p>Test</p>
@foreach (var i in items)
{
<h>item: </h>
<div>
<p>i</p>
</div>
}
<p>End Test</p>
</cshtml> |
@krwq |
Glad it works for you 😄 (still facepalming on my first response though) |
It was my typo mistake in first place :) .. Sleap time errors :D . |
XElement behaves bad with xml nodes that contain literal strings beside xml elements. In this case, XElement deletes all white spaces, including line separators, regardless of the reader and writer settings, and the method used to read the XElement content(using a reader, or ToString)
For example:
The resulting string in s will be:
The format is damaged because the c# string!. If you omitted it, every thing will work fine! Why?
The text was updated successfully, but these errors were encountered: