Search Unity

XmlDocument.LoadXML in WebGL throws NotSupportedException

Discussion in 'Web' started by Roywise, Oct 30, 2018.

  1. Roywise

    Roywise

    Joined:
    Jun 1, 2017
    Posts:
    68
    After debugging for some time I noticed that my usage of the XmlDocument.LoadXML(string) method in a WebGL build of my project throws a NotSupportedException when the XML string includes a XML DTD URL.

    Something along the lines of: <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE xml PUBLIC "-//Company//Product//EN" "XML DTD URL HERE">.


    Code (CSharp):
    1. NotSupportedException: The URI prefix is not recognized.
    2.   at System.Net.WebRequest.Create (System.Uri requestUri, System.Boolean useUriBase)

    Should this be considered a bug? It's not really a Unity bug I guess but feels like Unity could have warned me about it.
     
  2. poweremil

    poweremil

    Joined:
    Aug 3, 2018
    Posts:
    8
    It is because it uses reflection I believe to deserialize and Unity WebGL does not have support for reflection. I had this issue my self some time ago and I solved it by using XDocument.Load and manually reading each element and attributes.
     
  3. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    Which .NET profile are you using? In my application we deal with both serialising and deserialising XML but have found that when moving from the 3.5 target to 4.6 we started having issues in WebGL. However, when modifying the target from 4.6 to .NET Standard it actually resolved itself - it seems to have a different implementation for the Xml classes that avoids enough reflection calls (mainly the Reflection.Emit namespace) for it to work in WebGL.
     
  4. Roywise

    Roywise

    Joined:
    Jun 1, 2017
    Posts:
    68
    We're using the .NET 4.x Equivalent as Scripting Runtime Version and .NET 4.x as Api Compatibility Level. I haven't tried putting it on different settings because the Windows application requires the use of these and we'd like to keep those settings equal across all platforms.

    I didn't make it clear in the original post but I've worked around the problem by stripping the first part of the XML Data (XML Version and Doctype) using a simple string.Substring(int). Works as it should afterwards.