From ca7e4d535d6cc3d395ddae27ba452433f4bc8062 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 30 Jun 2025 19:42:24 +0000 Subject: [PATCH 1/6] Initial plan From 0ff241075aec31dedfc08351d21589db73522695 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 30 Jun 2025 20:00:34 +0000 Subject: [PATCH 2/6] Add XML roundtripping section with carriage return entity documentation Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../linq/preserve-white-space-serializing.md | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/docs/standard/linq/preserve-white-space-serializing.md b/docs/standard/linq/preserve-white-space-serializing.md index 714b546cb125c..7e9b8bd54ae07 100644 --- a/docs/standard/linq/preserve-white-space-serializing.md +++ b/docs/standard/linq/preserve-white-space-serializing.md @@ -25,3 +25,81 @@ The following methods in the and as an argument, then the method will format (indent) the serialized XML. In this case, all insignificant white space in the XML tree is discarded. If the method does take as an argument, then you can specify that the method not format (indent) the serialized XML. In this case, all white space in the XML tree is preserved. + +## Roundtripping XML with carriage return entities + +The whitespace preservation discussed in this article is different from XML roundtripping. When XML contains carriage return entities (` `), LINQ to XML's standard serialization might not preserve them in a way that allows perfect roundtripping. + +Consider the following example XML that contains carriage return entities: + +```xml +a +b +c +``` + +When you parse this XML with `XDocument.Parse()`, the root element's value becomes `"a\r\nb\nc\r"`. However, if you reserialize it using LINQ to XML methods, the carriage returns are not entitized: + +```csharp +string xmlWithCR = """a +b +c """; + +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: a +// b +// c + +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 +``` + +The values are different: the original was `"a\r\nb\nc\r"` but after roundtripping it becomes `"a\nb\nc\n"`. + +### Solution: Use XmlWriter with NewLineHandling.Entitize + +To achieve true XML roundtripping that preserves carriage return entities, use with set to : + +```csharp +string xmlWithCR = """a +b +c """; + +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: a +// b +// c + +// 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 +``` + +When you need to preserve carriage return entities for XML roundtripping, use with the appropriate instead of LINQ to XML's built-in serialization methods. + +For more information about and its settings, see . From f75a793818fd4db7b2a5806733ded9ed2c01e018 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 1 Jul 2025 16:14:40 +0000 Subject: [PATCH 3/6] Move code samples to snippet files and fix raw string literal format Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../linq/preserve-white-space-serializing.md | 54 +-------------- .../.gitignore | 2 + .../Program.cs | 69 +++++++++++++++++++ .../RoundtrippingProblem.cs | 19 +++++ .../RoundtrippingSolution.cs | 33 +++++++++ .../preserve-white-space-serializing.csproj | 10 +++ 6 files changed, 135 insertions(+), 52 deletions(-) create mode 100644 docs/standard/linq/snippets/preserve-white-space-serializing/.gitignore create mode 100644 docs/standard/linq/snippets/preserve-white-space-serializing/Program.cs create mode 100644 docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs create mode 100644 docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs create mode 100644 docs/standard/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj diff --git a/docs/standard/linq/preserve-white-space-serializing.md b/docs/standard/linq/preserve-white-space-serializing.md index 7e9b8bd54ae07..e8d2004aa7074 100644 --- a/docs/standard/linq/preserve-white-space-serializing.md +++ b/docs/standard/linq/preserve-white-space-serializing.md @@ -40,25 +40,7 @@ c When you parse this XML with `XDocument.Parse()`, the root element's value becomes `"a\r\nb\nc\r"`. However, if you reserialize it using LINQ to XML methods, the carriage returns are not entitized: -```csharp -string xmlWithCR = """a -b -c """; - -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: a -// b -// c - -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 -``` +:::code language="csharp" source="snippets/preserve-white-space-serializing/RoundtrippingProblem.cs"::: The values are different: the original was `"a\r\nb\nc\r"` but after roundtripping it becomes `"a\nb\nc\n"`. @@ -66,39 +48,7 @@ The values are different: the original was `"a\r\nb\nc\r"` but after roundtrippi To achieve true XML roundtripping that preserves carriage return entities, use with set to : -```csharp -string xmlWithCR = """a -b -c """; - -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: a -// b -// c - -// 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 -``` +:::code language="csharp" source="snippets/preserve-white-space-serializing/RoundtrippingSolution.cs"::: When you need to preserve carriage return entities for XML roundtripping, use with the appropriate instead of LINQ to XML's built-in serialization methods. diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/.gitignore b/docs/standard/linq/snippets/preserve-white-space-serializing/.gitignore new file mode 100644 index 0000000000000..cbbd0b5c02f84 --- /dev/null +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/.gitignore @@ -0,0 +1,2 @@ +bin/ +obj/ \ No newline at end of file diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/Program.cs b/docs/standard/linq/snippets/preserve-white-space-serializing/Program.cs new file mode 100644 index 0000000000000..d475ebbb5b2ae --- /dev/null +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/Program.cs @@ -0,0 +1,69 @@ +using System.Xml; +using System.Xml.Linq; + +// Example showing the roundtripping problem +static void DemonstrateRoundtrippingProblem() +{ + string xmlWithCR = """ + a + b + c + """; + + 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: a + // b + // c + + 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 = """ + a + b + c + """; + + 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: a + // b + // c + + // 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(); \ No newline at end of file diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs new file mode 100644 index 0000000000000..d8545633ade57 --- /dev/null +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs @@ -0,0 +1,19 @@ +string xmlWithCR = """ + a + b + c + """; + +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: a +// b +// c + +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 \ No newline at end of file diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs new file mode 100644 index 0000000000000..ca6f2b5348303 --- /dev/null +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs @@ -0,0 +1,33 @@ +string xmlWithCR = """ + a + b + c + """; + +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: a +// b +// c + +// 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 \ No newline at end of file diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj b/docs/standard/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj new file mode 100644 index 0000000000000..c28018dbfdd4c --- /dev/null +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + \ No newline at end of file From b6e19f2da386ffc57d73979bd3a9c4357cf83b6e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 1 Jul 2025 18:04:48 +0000 Subject: [PATCH 4/6] Fix code samples to compile in documentation environment and update target framework to net9.0 Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../.gitignore | 2 - .../Program.cs | 69 ------------------- .../RoundtrippingProblem.cs | 42 ++++++----- .../RoundtrippingSolution.cs | 67 ++++++++++-------- .../preserve-white-space-serializing.csproj | 2 +- 5 files changed, 66 insertions(+), 116 deletions(-) delete mode 100644 docs/standard/linq/snippets/preserve-white-space-serializing/.gitignore delete mode 100644 docs/standard/linq/snippets/preserve-white-space-serializing/Program.cs diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/.gitignore b/docs/standard/linq/snippets/preserve-white-space-serializing/.gitignore deleted file mode 100644 index cbbd0b5c02f84..0000000000000 --- a/docs/standard/linq/snippets/preserve-white-space-serializing/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -bin/ -obj/ \ No newline at end of file diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/Program.cs b/docs/standard/linq/snippets/preserve-white-space-serializing/Program.cs deleted file mode 100644 index d475ebbb5b2ae..0000000000000 --- a/docs/standard/linq/snippets/preserve-white-space-serializing/Program.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System.Xml; -using System.Xml.Linq; - -// Example showing the roundtripping problem -static void DemonstrateRoundtrippingProblem() -{ - string xmlWithCR = """ - a - b - c - """; - - 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: a - // b - // c - - 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 = """ - a - b - c - """; - - 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: a - // b - // c - - // 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(); \ No newline at end of file diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs index d8545633ade57..fcde57736c800 100644 --- a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs @@ -1,19 +1,29 @@ -string xmlWithCR = """ - a - b - c - """; +using System; +using System.Linq; +using System.Xml.Linq; -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 +class Program +{ + static void Main() + { + string xmlWithCR = """ + a + b + c + """; -string reserialized = doc.ToString(SaveOptions.DisableFormatting); -Console.WriteLine($"Reserialized XML: {reserialized}"); -// Output: a -// b -// c + 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 -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 \ No newline at end of file + string reserialized = doc.ToString(SaveOptions.DisableFormatting); + Console.WriteLine($"Reserialized XML: {reserialized}"); + // Output: a + // b + // c + + 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 + } +} \ No newline at end of file diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs index ca6f2b5348303..8c94b698edd0a 100644 --- a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs @@ -1,33 +1,44 @@ -string xmlWithCR = """ - a - b - c - """; +using System; +using System.IO; +using System.Xml; +using System.Xml.Linq; -XDocument doc = XDocument.Parse(xmlWithCR); - -// Create XmlWriter settings with NewLineHandling.Entitize -XmlWriterSettings settings = new XmlWriterSettings +class Program { - NewLineHandling = NewLineHandling.Entitize, - OmitXmlDeclaration = true -}; + static void Main() + { + string xmlWithCR = """ + a + b + c + """; -// Serialize using XmlWriter -using StringWriter stringWriter = new StringWriter(); -using (XmlWriter writer = XmlWriter.Create(stringWriter, settings)) -{ - doc.WriteTo(writer); -} + 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: a -// b -// c + string roundtrippedXml = stringWriter.ToString(); + Console.WriteLine($"Roundtripped XML: {roundtrippedXml}"); + // Output: a + // b + // c -// 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 \ No newline at end of file + // 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 + } +} \ No newline at end of file diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj b/docs/standard/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj index c28018dbfdd4c..3477df059b152 100644 --- a/docs/standard/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj @@ -2,7 +2,7 @@ Exe - net8.0 + net9.0 enable enable From 0f439995fddd2bed924a873af281ef1e52b6a2c5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 1 Jul 2025 18:35:55 +0000 Subject: [PATCH 5/6] Restructure code samples to use single Program class with Example methods and fix target framework Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../RoundtrippingProblem.cs | 15 +++++++++++++++ .../RoundtrippingSolution.cs | 4 ++-- .../preserve-white-space-serializing.csproj | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs index fcde57736c800..a672d6fedf0f4 100644 --- a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs @@ -5,6 +5,21 @@ 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() { string xmlWithCR = """ a diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs index 8c94b698edd0a..f2352adeb4dcd 100644 --- a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs @@ -3,9 +3,9 @@ using System.Xml; using System.Xml.Linq; -class Program +public static class RoundtrippingSolution { - static void Main() + public static void Example() { string xmlWithCR = """ a diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj b/docs/standard/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj index 3477df059b152..c28018dbfdd4c 100644 --- a/docs/standard/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/preserve-white-space-serializing.csproj @@ -2,7 +2,7 @@ Exe - net9.0 + net8.0 enable enable From 0d5880353d1f76eac2bbc2d8a1ccb58bf68ab17e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 1 Jul 2025 18:56:05 +0000 Subject: [PATCH 6/6] Add snippet tags to filter displayed code sections Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- docs/standard/linq/preserve-white-space-serializing.md | 4 ++-- .../preserve-white-space-serializing/RoundtrippingProblem.cs | 2 ++ .../preserve-white-space-serializing/RoundtrippingSolution.cs | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/standard/linq/preserve-white-space-serializing.md b/docs/standard/linq/preserve-white-space-serializing.md index e8d2004aa7074..7d14477145f43 100644 --- a/docs/standard/linq/preserve-white-space-serializing.md +++ b/docs/standard/linq/preserve-white-space-serializing.md @@ -40,7 +40,7 @@ c When you parse this XML with `XDocument.Parse()`, the root element's value becomes `"a\r\nb\nc\r"`. However, if you reserialize it using LINQ to XML methods, the carriage returns are not entitized: -:::code language="csharp" source="snippets/preserve-white-space-serializing/RoundtrippingProblem.cs"::: +:::code language="csharp" source="snippets/preserve-white-space-serializing/RoundtrippingProblem.cs" Id="XmlRoundTrip"::: The values are different: the original was `"a\r\nb\nc\r"` but after roundtripping it becomes `"a\nb\nc\n"`. @@ -48,7 +48,7 @@ The values are different: the original was `"a\r\nb\nc\r"` but after roundtrippi To achieve true XML roundtripping that preserves carriage return entities, use with set to : -:::code language="csharp" source="snippets/preserve-white-space-serializing/RoundtrippingSolution.cs"::: +:::code language="csharp" source="snippets/preserve-white-space-serializing/RoundtrippingSolution.cs" Id="XmlRoundTripFix"::: When you need to preserve carriage return entities for XML roundtripping, use with the appropriate instead of LINQ to XML's built-in serialization methods. diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs index a672d6fedf0f4..81637b26f46c4 100644 --- a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingProblem.cs @@ -21,6 +21,7 @@ public static class RoundtrippingProblem { public static void Example() { + // string xmlWithCR = """ a b @@ -40,5 +41,6 @@ public static void Example() 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 + // } } \ No newline at end of file diff --git a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs index f2352adeb4dcd..04edd2e585cb4 100644 --- a/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs +++ b/docs/standard/linq/snippets/preserve-white-space-serializing/RoundtrippingSolution.cs @@ -7,6 +7,7 @@ public static class RoundtrippingSolution { public static void Example() { + // string xmlWithCR = """ a b @@ -39,6 +40,7 @@ public static void Example() XDocument roundtrippedDoc = XDocument.Parse(roundtrippedXml); bool valuesMatch = doc.Root!.Value == roundtrippedDoc.Root!.Value; Console.WriteLine($"Values match after roundtripping: {valuesMatch}"); + // // Output: True } } \ No newline at end of file