Search code examples
wcfbiztalkbiztalk-2010

BizTalk 2010 WCF Remove processing instruction


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.


Solution

  • 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);
      }