Skip to content

Commit

Permalink
Merge pull request #15 from hughesjs/ignore-xmlns
Browse files Browse the repository at this point in the history
Fix: Ignore xmlns
  • Loading branch information
hughesjs authored May 6, 2024
2 parents 764986e + 20cbd48 commit 168088b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,18 @@ public void CanDeserialiseNonRootObject()
Image image = _uddfDeserialiser.Deserialise<Image>(xml);
image.Id.ShouldBe("img_flatfeet");
image.ObjectName.ShouldBe("flatfeet.jpg");
}
}

[Fact]
public void CanDeserialiseWithNamespace()
{
const string xml = """
<uddf version="3.2.0" xmlns="http://www.streit.cc/uddf/3.2/">
<mediadata>
</mediadata>
</uddf>
""";
Should.NotThrow(() => _uddfDeserialiser.Deserialise<UniversalDiveDataFormatRoot>(xml));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Xml;

namespace UniversalDiveDataFormat.Services;

// This is needed because not all sources set the xmlns correctly
public class NamespaceIgnorantXmlTextReader : XmlTextReader
{
public NamespaceIgnorantXmlTextReader(TextReader reader): base(reader) { }
public NamespaceIgnorantXmlTextReader(Stream stream): base(stream) { }

public override string NamespaceURI => string.Empty;
}
10 changes: 8 additions & 2 deletions src/UniversalDiveDataFormat/Services/UddfDeserialiser.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Xml;
using System.Xml.Serialization;
using UniversalDiveDataFormat.Models;

Expand All @@ -6,6 +7,7 @@ namespace UniversalDiveDataFormat.Services;
public class UddfDeserialiser: IUddfDeserialiser
{
private readonly LinkResolutionService _linkResolutionService;

public UddfDeserialiser(LinkResolutionService linkResolutionService)
{
_linkResolutionService = linkResolutionService;
Expand All @@ -16,15 +18,19 @@ public UddfDeserialiser(LinkResolutionService linkResolutionService)
public T Deserialise<T>(TextReader reader) where T : UddfModel
{
XmlSerializer serializer = new(typeof(T));
T obj = (T)serializer.Deserialize(reader)!; // This should throw if the xml is invalid
XmlReader xmlReader = new NamespaceIgnorantXmlTextReader(reader);

T obj = (T)serializer.Deserialize(xmlReader)!; // This should throw if the xml is invalid
_linkResolutionService.ResolveAllLinksInObjectGraph(obj);
return obj;
}

public T Deserialise<T>(Stream stream) where T : UddfModel
{
XmlSerializer serializer = new(typeof(T));
T obj = (T)serializer.Deserialize(stream)!; // This should throw if the xml is invalid
XmlReader xmlReader = new NamespaceIgnorantXmlTextReader(stream);

T obj = (T)serializer.Deserialize(xmlReader)!; // This should throw if the xml is invalid
_linkResolutionService.ResolveAllLinksInObjectGraph(obj);
return obj;
}
Expand Down

0 comments on commit 168088b

Please sign in to comment.