-
Notifications
You must be signed in to change notification settings - Fork 6k
Add XML roundtripping documentation for carriage return entities #47034
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ca7e4d5
Initial plan
Copilot 0ff2410
Add XML roundtripping section with carriage return entity documentation
Copilot f75a793
Move code samples to snippet files and fix raw string literal format
Copilot b6e19f2
Fix code samples to compile in documentation environment and update t…
Copilot 0f43999
Restructure code samples to use single Program class with Example met…
Copilot 0d58803
Add snippet tags to filter displayed code sections
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
docs/standard/linq/snippets/preserve-white-space-serializing/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bin/ | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
obj/ |
69 changes: 69 additions & 0 deletions
69
docs/standard/linq/snippets/preserve-white-space-serializing/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System.Xml; | ||
using System.Xml.Linq; | ||
|
||
// Example showing the roundtripping problem | ||
static void DemonstrateRoundtrippingProblem() | ||
{ | ||
string xmlWithCR = """ | ||
<x xml:space="preserve">a
 | ||
b | ||
c
</x> | ||
"""; | ||
|
||
XDocument doc = XDocument.Parse(xmlWithCR); | ||
Console.WriteLine($"Original parsed value: {string.Join("", doc.Root.Value.Select(c => c == '\r' ? "\\r" : c == '\n' ? "\\n" : c.ToString()))}"); | ||
// Output: a\r\nb\nc\r | ||
|
||
string reserialized = doc.ToString(SaveOptions.DisableFormatting); | ||
Console.WriteLine($"Reserialized XML: {reserialized}"); | ||
// Output: <x xml:space="preserve">a | ||
// b | ||
// c</x> | ||
|
||
XDocument reparsed = XDocument.Parse(reserialized); | ||
Console.WriteLine($"Reparsed value: {string.Join("", reparsed.Root.Value.Select(c => c == '\r' ? "\\r" : c == '\n' ? "\\n" : c.ToString()))}"); | ||
// Output: a\nb\nc\n | ||
} | ||
|
||
// Example showing the roundtripping solution | ||
static void DemonstrateRoundtrippingSolution() | ||
{ | ||
string xmlWithCR = """ | ||
<x xml:space="preserve">a
 | ||
b | ||
c
</x> | ||
"""; | ||
|
||
XDocument doc = XDocument.Parse(xmlWithCR); | ||
|
||
// Create XmlWriter settings with NewLineHandling.Entitize | ||
XmlWriterSettings settings = new XmlWriterSettings | ||
{ | ||
NewLineHandling = NewLineHandling.Entitize, | ||
OmitXmlDeclaration = true | ||
}; | ||
|
||
// Serialize using XmlWriter | ||
using StringWriter stringWriter = new StringWriter(); | ||
using (XmlWriter writer = XmlWriter.Create(stringWriter, settings)) | ||
{ | ||
doc.WriteTo(writer); | ||
} | ||
|
||
string roundtrippedXml = stringWriter.ToString(); | ||
Console.WriteLine($"Roundtripped XML: {roundtrippedXml}"); | ||
// Output: <x xml:space="preserve">a
 | ||
// b | ||
// c
</x> | ||
|
||
// Verify roundtripping preserves the original value | ||
XDocument roundtrippedDoc = XDocument.Parse(roundtrippedXml); | ||
bool valuesMatch = doc.Root.Value == roundtrippedDoc.Root.Value; | ||
Console.WriteLine($"Values match after roundtripping: {valuesMatch}"); | ||
// Output: True | ||
} | ||
|
||
// Run both examples | ||
DemonstrateRoundtrippingProblem(); | ||
Console.WriteLine(); | ||
DemonstrateRoundtrippingSolution(); |
19 changes: 19 additions & 0 deletions
19
docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
string xmlWithCR = """ | ||
Check failure on line 1 in docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs
|
||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<x xml:space="preserve">a
 | ||
b | ||
c
</x> | ||
"""; | ||
|
||
XDocument doc = XDocument.Parse(xmlWithCR); | ||
Console.WriteLine($"Original parsed value: {string.Join("", doc.Root.Value.Select(c => c == '\r' ? "\\r" : c == '\n' ? "\\n" : c.ToString()))}"); | ||
// Output: a\r\nb\nc\r | ||
|
||
string reserialized = doc.ToString(SaveOptions.DisableFormatting); | ||
Console.WriteLine($"Reserialized XML: {reserialized}"); | ||
// Output: <x xml:space="preserve">a | ||
// b | ||
// c</x> | ||
|
||
XDocument reparsed = XDocument.Parse(reserialized); | ||
Console.WriteLine($"Reparsed value: {string.Join("", reparsed.Root.Value.Select(c => c == '\r' ? "\\r" : c == '\n' ? "\\n" : c.ToString()))}"); | ||
// Output: a\nb\nc\n |
33 changes: 33 additions & 0 deletions
33
docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
string xmlWithCR = """ | ||
Check failure on line 1 in docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs
|
||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<x xml:space="preserve">a
 | ||
b | ||
c
</x> | ||
"""; | ||
|
||
XDocument doc = XDocument.Parse(xmlWithCR); | ||
|
||
// Create XmlWriter settings with NewLineHandling.Entitize | ||
XmlWriterSettings settings = new XmlWriterSettings | ||
{ | ||
NewLineHandling = NewLineHandling.Entitize, | ||
OmitXmlDeclaration = true | ||
}; | ||
|
||
// Serialize using XmlWriter | ||
using StringWriter stringWriter = new StringWriter(); | ||
using (XmlWriter writer = XmlWriter.Create(stringWriter, settings)) | ||
{ | ||
doc.WriteTo(writer); | ||
} | ||
|
||
string roundtrippedXml = stringWriter.ToString(); | ||
Console.WriteLine($"Roundtripped XML: {roundtrippedXml}"); | ||
// Output: <x xml:space="preserve">a
 | ||
// b | ||
// c
</x> | ||
|
||
// Verify roundtripping preserves the original value | ||
XDocument roundtrippedDoc = XDocument.Parse(roundtrippedXml); | ||
bool valuesMatch = doc.Root.Value == roundtrippedDoc.Root.Value; | ||
Console.WriteLine($"Values match after roundtripping: {valuesMatch}"); | ||
// Output: True |
10 changes: 10 additions & 0 deletions
10
...rd/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The language “might not” should be “does not” @BillWagner .