2

I need to do download an XML file from a public website (http://www.tcmb.gov.tr/kurlar/201707/10072017.xml) to get exchange rates. But I have a problem since the XML contains an xml-stylesheet processing instruction.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="isokur.xsl"?>
<Tarih_Date Tarih="07.07.2017" Date="07/07/2017"  Bulten_No="2017/131" >

I use a WCF-Custom port with webHttpBindng and BizTalk REST starter kit from bLogical. Everything works fine, but when I try to parse the incoming xml, I get an error on that processing instruction.

System.Xml.XmlException: Processing instructions (other than the XML declaration) and DTDs are not supported. Line 2, position 2.

I'm not sure what the best way would be to fix this. I tried to follow this guide WCF Errors on XML Deserialization but it still fails when I try to access the message content using the CreateBufferedCopy method.

 using (var readStream = new System.IO.MemoryStream())
 {
      using (var buffer = reply.CreateBufferedCopy(int.MaxValue))
      {
         buffer.WriteMessage(readStream);
      }

      readStream.Position = 0;
      xdoc.Load(readStream);
 }

Does anybody know how I can access the content of my message without actually parsing the XML? I'm just trying to find a way to either remove that line or make the parser ignore it.

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Tim D'haeyer
  • 584
  • 6
  • 19
  • What is the error you're getting when using the CreateBufferedCopy() method? – zurebe-pieter Jul 13 '17 at 08:31
  • Same error as mentioned above. System.Xml.XmlException: Processing instructions (other than the XML declaration) and DTDs are not supported. Line 2, position 2. – Tim D'haeyer Jul 13 '17 at 09:25
  • Where are you executing that code? In the Decode section of a Receive Pipeline? Also that code doesn't seem to remove the Stylesheet information – Dijkgraaf Jul 14 '17 at 21:57
  • This is a custom client message inspector that I use in my WCF configuration. I only posted a part of the code since I don't even reach the step where I can remove that line. If you want more info, just follow the link. That's the post I used to create the message inspector. – Tim D'haeyer Jul 20 '17 at 11:48

1 Answers1

0

I found the solution myself in the end. Instead of a message inspector, I created a Message Encoder based on the CustomTextMessageEncoder that you can find online. In the ReadMessage method I just added a little bit of code

  public override Message ReadMessage(System.IO.Stream stream, int maxSizeOfHeaders, string contentType)
  {
      XmlReaderSettings xsettings = new XmlReaderSettings();
      xsettings.IgnoreProcessingInstructions = true;
      XmlReader reader = XmlReader.Create(stream,xsettings);
      return Message.CreateMessage(reader, maxSizeOfHeaders, this.MessageVersion);
  }
Tim D'haeyer
  • 584
  • 6
  • 19