0

I'm trying to read a WSDL from a URL to dynamically generate the proxy for the WCF service. This is my code:

XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(description)) 
if (ServiceDescription.CanRead(xmlTextReader))
{
    ...
}

I get an XmlException from method ServiceDescription.CanRead.
The error massage is "Data at the root level is invalid. Line 1, position 1".

Browsing the WDSL URL in IE, I can see the following tag at the start before tag <wsdl:definitions ...> ... </wsdl:definitions> which doesn't appear in chrome.

<?xml version="1.0" encoding="UTF-8"?>

Could that be the issue? but I suppose ServiceDescription.CanRead should be able to recognise that. Any hints would be appreciated.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
Hamid Heydarian
  • 802
  • 9
  • 16
  • What about inspecting the XML that's being read? `CanRead` is just calling `CanDeserialize` on an inner instance of XmlSerializer and returning that value. – Scott Hannen May 04 '17 at 02:06
  • 1
    And the xml declaration you're seeing in IE is optional but valid. [http://stackoverflow.com/questions/7007427/does-a-valid-xml-file-require-an-xml-declaration](http://stackoverflow.com/questions/7007427/does-a-valid-xml-file-require-an-xml-declaration) – Scott Hannen May 04 '17 at 02:09
  • This could be it - an extra hidden character that prevents parsing of the XML. [http://stackoverflow.com/questions/17947238/why-data-at-the-root-level-is-invalid-line-1-position-1-for-xml-document/24513288](http://stackoverflow.com/questions/17947238/why-data-at-the-root-level-is-invalid-line-1-position-1-for-xml-document/24513288) – Scott Hannen May 04 '17 at 02:11
  • Try pasting the code from my answer above the first line of code in your post. I don't know if it's the fix in this case but maybe it's worth a shot. – Scott Hannen May 04 '17 at 02:36

1 Answers1

1

Try adding this before the first line included in your question:

var byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (description.StartsWith(byteOrderMarkUtf8))
{
    var lastIndexOfUtf8 = byteOrderMarkUtf8.Length - 1;
    description = description.Remove(0, lastIndexOfUtf8);
}

Borrowed from here.

Community
  • 1
  • 1
Scott Hannen
  • 27,588
  • 3
  • 45
  • 62