-
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 all 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
46 changes: 46 additions & 0 deletions
46
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,46 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
|
||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
Console.WriteLine("XML Roundtripping Examples"); | ||
Console.WriteLine("=========================="); | ||
|
||
Console.WriteLine("\n1. Roundtripping Problem:"); | ||
RoundtrippingProblem.Example(); | ||
|
||
Console.WriteLine("\n2. Roundtripping Solution:"); | ||
RoundtrippingSolution.Example(); | ||
} | ||
} | ||
|
||
public static class RoundtrippingProblem | ||
{ | ||
public static void Example() | ||
{ | ||
// <XmlRoundTrip> | ||
string xmlWithCR = """ | ||
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 | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// </XmlRoundTrip> | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
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,46 @@ | ||
using System; | ||
using System.IO; | ||
using System.Xml; | ||
using System.Xml.Linq; | ||
|
||
public static class RoundtrippingSolution | ||
{ | ||
public static void Example() | ||
{ | ||
// <XmlRoundTripFix> | ||
string xmlWithCR = """ | ||
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}"); | ||
// </XmlRoundTripFix> | ||
// Output: True | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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> |
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 .