I’m trying to parse an XML document on Windows Phone platform (WP8.1).
I’m using Unity version 5.1.2f.
Although I’ve seen at least one post with same issue from Jun 2014
(XML.load on WP8 - Unity Engine - Unity Discussions)
I’m using defines as below (Although I don’t know when NETFX_CORE gets defined or if it is even correct or valid anymore. I saw it on numerous XML related issues in this forum, so using them)
#if NETFX_CORE && !UNITY_EDITOR
using XmlReader = WinRTLegacy.Xml.XmlReader;
#else
using XmlReader = System.Xml.XmlReader;
The code in question is like:
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlString);
I’ve triple checked the validity of xmlString externally and it is a valid XML. The code runs fine as it is on Android and in Editor.
The XML in question does have a DTD header though. Exception I get is:
Exception: Object reference not set to an instance of an object.
Type: System.NullReferenceException
Module: WinRTLegacy
InnerException: <No Data>
AdditionalInfo:<No Data>
at System.Xml.XmlDocumentType.ImportFromDTD()
at System.Xml.XmlDocumentType..ctor(String name, String publicId, String systemId, String internalSubset, XmlDocument doc)
at System.Xml.XmlDocument.CreateDocumentType(String name, String publicId, String systemId, String internalSubset)
at System.Xml.XmlDocument.ReadNodeCore(XmlReader reader)
at System.Xml.XmlDocument.ReadNode(XmlReader reader)
at System.Xml.XmlDocument.Load(XmlReader xmlReader)
at System.Xml.XmlDocument.LoadXml(String xml)
So next I tried using XmlReader:
var xmlReader = XmlReader.Create(new System.IO.StringReader(xmlString));
xml.Load(xmlReader);
which got me:
Exception: Method not found: 'Void System.Xml.XmlDocument.Load(System.Xml.XmlReader)'.
Type: System.MissingMethodException
Module: Assembly-CSharp
InnerException: <No Data>
AdditionalInfo:<No Data>
So how come the System.Xml.XmlDocument.Load(XmlReader xmlReader) call works in first stack trace but if I tried to call manually gives me MissingMethodException under same defines in my class. I MUST NOT be using correct defines. What is a correct define in place of NETFX_CORE?
Also for first Null reference, which is the real issue, is it because of DTD tag ()? I am only guessing from the System.Xml.XmlDocumentType.ImportFromDTD() frame in first stack trace.
I checked what I could from WinRTLegacy in ILSpy and:
System.Xml.XmlDocumentType constructor seems to be creating a new instance of Mono.Xml2.XmlTextReader and then doing:
this.dtd = xmlTextReader.DTD;
But that property in XmlTextReader seems to be defined as:
internal DTDObjectModel DTD
{
get
{
return null;
}
}
And then in ImportFromDTD() it has:
using (IEnumerator<DTDNode> enumerator = this.DTD.EntityDecls.Values.GetEnumerator())
{
......................
}
(DTD is wrapping dtd)
I could be way, way off here as I’m looking with ILSpy. So this may be discarded.
However if the DTDs are not supported then can we NOT have a hard fail like this. (skip the call to create document type? OR Just skip over the DOCTYPE element or create a default xmlNode. etc.)
I really would like to have the DTD in XMLs as some external tools in pipeline also use the same XML and XML itself is generated by third party tools. If parsing support is not there then it’s fine but if the XML itself could get through then that is more than enough.
Is there any other code flow I can use to not run into this as a workaround?