From 20cbd48982a0d64e0415cae768b100a4b0903d64 Mon Sep 17 00:00:00 2001 From: James Hughes Date: Tue, 7 May 2024 00:29:59 +0100 Subject: [PATCH] Ignore xmlns --- .../Services/UddfDeserialiserTests.cs | 15 ++++++++++++++- .../Services/NamespaceIgnorantXmlTextReader.cs | 12 ++++++++++++ .../Services/UddfDeserialiser.cs | 10 ++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/UniversalDiveDataFormat/Services/NamespaceIgnorantXmlTextReader.cs diff --git a/src/UniversalDiveDataFormat.Tests/Services/UddfDeserialiserTests.cs b/src/UniversalDiveDataFormat.Tests/Services/UddfDeserialiserTests.cs index 7e5e20f..e04581e 100644 --- a/src/UniversalDiveDataFormat.Tests/Services/UddfDeserialiserTests.cs +++ b/src/UniversalDiveDataFormat.Tests/Services/UddfDeserialiserTests.cs @@ -184,5 +184,18 @@ public void CanDeserialiseNonRootObject() Image image = _uddfDeserialiser.Deserialise(xml); image.Id.ShouldBe("img_flatfeet"); image.ObjectName.ShouldBe("flatfeet.jpg"); - } + } + + [Fact] + public void CanDeserialiseWithNamespace() + { + const string xml = """ + + + + + + """; + Should.NotThrow(() => _uddfDeserialiser.Deserialise(xml)); + } } \ No newline at end of file diff --git a/src/UniversalDiveDataFormat/Services/NamespaceIgnorantXmlTextReader.cs b/src/UniversalDiveDataFormat/Services/NamespaceIgnorantXmlTextReader.cs new file mode 100644 index 0000000..76a0ea9 --- /dev/null +++ b/src/UniversalDiveDataFormat/Services/NamespaceIgnorantXmlTextReader.cs @@ -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; +} \ No newline at end of file diff --git a/src/UniversalDiveDataFormat/Services/UddfDeserialiser.cs b/src/UniversalDiveDataFormat/Services/UddfDeserialiser.cs index dac603d..e4657ab 100644 --- a/src/UniversalDiveDataFormat/Services/UddfDeserialiser.cs +++ b/src/UniversalDiveDataFormat/Services/UddfDeserialiser.cs @@ -1,3 +1,4 @@ +using System.Xml; using System.Xml.Serialization; using UniversalDiveDataFormat.Models; @@ -6,6 +7,7 @@ namespace UniversalDiveDataFormat.Services; public class UddfDeserialiser: IUddfDeserialiser { private readonly LinkResolutionService _linkResolutionService; + public UddfDeserialiser(LinkResolutionService linkResolutionService) { _linkResolutionService = linkResolutionService; @@ -16,7 +18,9 @@ public UddfDeserialiser(LinkResolutionService linkResolutionService) public T Deserialise(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; } @@ -24,7 +28,9 @@ public T Deserialise(TextReader reader) where T : UddfModel public T Deserialise(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; }