Skip to content

Commit ddde13f

Browse files
Fix XML literals to be XML-decoded when converted to C# strings
This updates `XmlExpressionConverter.cs` to use `ValueText` instead of `Text` when converting XML strings and text nodes so that XML entities like `&lt;` are correctly evaluated as their decoded values. The test expectations have been updated appropriately to match the resulting actual string outputs which naturally produce the raw decoded literal character escapes like `\n` instead of verbatim multiline formats. A unit test proving the fix for the reported issue is also included. Co-authored-by: GrahamTheCoder <[email protected]>
1 parent 2c2b64e commit ddde13f

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

CodeConverter/CSharp/XmlExpressionConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public async Task<CSharpSyntaxNode> ConvertXmlAttributeAsync(VBasic.Syntax.XmlAt
6464
}
6565

6666
public async Task<CSharpSyntaxNode> ConvertXmlStringAsync(VBasic.Syntax.XmlStringSyntax node) =>
67-
CommonConversions.Literal(string.Join("", node.TextTokens.Select(b => b.Text)));
67+
CommonConversions.Literal(string.Join("", node.TextTokens.Select(b => b.ValueText)));
6868

6969
public async Task<CSharpSyntaxNode> ConvertXmlTextAsync(VBSyntax.XmlTextSyntax node) =>
70-
CommonConversions.Literal(string.Join("", node.TextTokens.Select(b => b.Text)));
70+
CommonConversions.Literal(string.Join("", node.TextTokens.Select(b => b.ValueText)));
7171

7272
public async Task<CSharpSyntaxNode> ConvertXmlCDataSectionAsync(VBSyntax.XmlCDataSectionSyntax node)
7373
{

Tests/CSharp/ExpressionTests/XmlExpressionTests.cs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -120,26 +120,13 @@ private void TestMethod()
120120
new XElement(""Author"", ""Garghentini, Davide""),
121121
new XElement(""Title"", ""XML Developer's Guide""),
122122
new XElement(""Price"", ""44.95""),
123-
new XElement(""Description"", @""
124-
An in-depth look at creating applications
125-
with "", new XElement(""technology"", ""XML""), @"". For
126-
"", new XElement(""audience"", ""beginners""), @"" or
127-
"", new XElement(""audience"", ""advanced""), @"" developers.
128-
"")
123+
new XElement(""Description"", ""\n An in-depth look at creating applications\n with "", new XElement(""technology"", ""XML""), "". For\n "", new XElement(""audience"", ""beginners""), "" or\n "", new XElement(""audience"", ""advanced""), "" developers.\n "")
129124
),
130125
new XElement(""Book"", new XAttribute(""id"", ""bk331""),
131126
new XElement(""Author"", ""Spencer, Phil""),
132127
new XElement(""Title"", ""Developing Applications with Visual Basic .NET""),
133128
new XElement(""Price"", ""45.95""),
134-
new XElement(""Description"", @""
135-
Get the expert insights, practical code samples,
136-
and best practices you need
137-
to advance your expertise with "", new XElement(""technology"", @""Visual
138-
Basic .NET""), @"".
139-
Learn how to create faster, more reliable applications
140-
based on professional,
141-
pragmatic guidance by today's top "", new XElement(""audience"", ""developers""), @"".
142-
"")
129+
new XElement(""Description"", ""\n Get the expert insights, practical code samples,\n and best practices you need\n to advance your expertise with "", new XElement(""technology"", ""Visual\n Basic .NET""), "".\n Learn how to create faster, more reliable applications\n based on professional,\n pragmatic guidance by today's top "", new XElement(""audience"", ""developers""), "".\n "")
143130
)
144131
)
145132
@@ -373,19 +360,9 @@ internal partial class TestClass
373360
{
374361
private void TestMethod()
375362
{
376-
string processedHtml = new XCData(@""
377-
<!DOCTYPE html PUBLIC """"-//W3C//DTD XHTML 1.0 Transitional//EN"""" """"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"""">
378-
<html xmlns=""""http://www.w3.org/1999/xhtml"""">
379-
<head>
380-
<meta http-equiv=""""Content-Type"""" content=""""text/html; charset=utf-8"""" /><title>
381-
</head>
382-
<body>
383-
<p class=""""cs95E872D0""""><span class=""""cs9D249CCB"""">&nbsp;Regards,</span></p>
384-
</body>
385-
</html>
386-
"").Value;
363+
string processedHtml = new XCData(""\n<!DOCTYPE html PUBLIC \""-//W3C//DTD XHTML 1.0 Transitional//EN\"" \""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"">\n<html xmlns=\""http://www.w3.org/1999/xhtml\"">\n\t<head>\n\t\t<meta http-equiv=\""Content-Type\"" content=\""text/html; charset=utf-8\"" /><title>\n\t</head>\n\t<body>\n\t\t<p class=\""cs95E872D0\""><span class=\""cs9D249CCB\"">&nbsp;Regards,</span></p>\n </body>\n</html>\n "").Value;
387364
}
388-
}");
365+
}", incompatibleWithAutomatedCommentTesting: true);
389366

390367
}
391368
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using ICSharpCode.CodeConverter.Tests.TestRunners;
2+
using Xunit;
3+
using System.Threading.Tasks;
4+
5+
namespace ICSharpCode.CodeConverter.Tests.CSharp.ExpressionTests;
6+
7+
public class XmlExpressionTestsLocal : ConverterTestBase
8+
{
9+
[Fact]
10+
public async Task XmlLiteralDecodingAsync()
11+
{
12+
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
13+
Private Sub TestMethod()
14+
Dim v = <xml>&lt;</xml>.Value
15+
End Sub
16+
End Class", @"using System.Xml.Linq;
17+
18+
internal partial class TestClass
19+
{
20+
private void TestMethod()
21+
{
22+
string v = new XElement(""xml"", ""<"").Value;
23+
}
24+
}");
25+
}
26+
}

0 commit comments

Comments
 (0)