Skip to content

Commit

Permalink
Accounted for successive CDATA blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-E-angelo committed Sep 15, 2020
1 parent 55868aa commit ae1b01d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
30 changes: 28 additions & 2 deletions src/ExtendedXmlSerializer/ExtensionModel/Xml/XmlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ExtendedXmlSerializer.ContentModel.Format;
using ExtendedXmlSerializer.ContentModel.Identification;
using System.Reflection;
using System.Text;
using System.Xml;

namespace ExtendedXmlSerializer.ExtensionModel.Xml
Expand Down Expand Up @@ -43,7 +44,6 @@ public string Content()
{
switch (_reader.NodeType)
{

case XmlNodeType.Attribute:
return _reader.Value;
default:
Expand All @@ -62,16 +62,42 @@ public string Content()

var result = isNull ? null : _reader.Value;

if (!string.IsNullOrEmpty(result) || _reader.NodeType == XmlNodeType.CDATA)
if (_reader.NodeType == XmlNodeType.CDATA)
{
return CharacterData(result);
}
else if (!string.IsNullOrEmpty(result))
{
_reader.Read();
Set();
}


return result;
}
}

string CharacterData(string result)
{
_reader.Read();
Set();

if (_reader.NodeType == XmlNodeType.CDATA)
{
var builder = new StringBuilder(result);
while (_reader.NodeType == XmlNodeType.CDATA)
{
builder.Append(_reader.Value);
_reader.Read();
Set();
}

return builder.ToString();
}

return result;
}

public void Set() => _reader.MoveToContent();

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public string LastName
}
}

private List<SampleItem> listOfItems = null;
List<SampleItem> listOfItems;

public List<SampleItem> ListOfItems
{
Expand Down
36 changes: 36 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue447Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.ContentModel.Content;
using ExtendedXmlSerializer.Tests.ReportedIssues.Support;
using FluentAssertions;
using JetBrains.Annotations;
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
public sealed class Issue447Tests
{
[Fact]
public void Verify()
{
const string content = @"<?xml version=""1.0"" encoding=""utf-8""?><Issue447Tests-Subject xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ReportedIssues;assembly=ExtendedXmlSerializer.Tests.ReportedIssues""><Message><![CDATA[Hello ]]><![CDATA[world!]]></Message><Message2><![CDATA[Not ]]><![CDATA[this.]]></Message2></Issue447Tests-Subject>";

var serializer = new ConfigurationContainer().Create()
.ForTesting();

serializer.Deserialize<Subject>(content)
.Message.Should().Be("Hello world!");

serializer.Deserialize<Subject>(content)
.Message2.Should().Be("Not this.");
}

sealed class Subject
{
[Verbatim]
public string Message { get; [UsedImplicitly] set; }

[Verbatim]
public string Message2 { get; [UsedImplicitly] set; }
}
}
}

0 comments on commit ae1b01d

Please sign in to comment.