none
Parse though a XML Docuemnt / webservice feed and remove ampersand RRS feed

  • Question

  • Hello,

    I'm trying to write a c# program that will read the (soap) results given from a webservice.

    I have been trying it with a test XML (given to me by the providers ) for the time being, and have got it displaying the data, BUT, only after I have gone through and removed the & 's .

    I obviously cannot do this with the response, so, I was trying to "fix" the data before I parse it.

    the data looks like

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <getRRS xmlns="http://supplierURL/"> <getRRSR> <cORR> <cType>DBL</cType> <nType>DOUBLE (TWIN/DOUBLE)</nType> <cRF>01-WL</cRF> <nRF>CLASSIC</nRF> <cORRR> <cRate>BAR</cRate> <nRate>BAR</nRate> <tRate>G</tRate> <beginDate>05/04/2016</beginDate> <endDate>31/12/2016</endDate> <cur>EUR</cur> <sSS>S</sSS> <bCB>BB</bCB> <maximumA>2</maximumA> <minimumA>2</minimumA> <maximumC>1</maximumC> <maximumI>1</maximumI> <maximumP>3</maximumP> <rP/> <cOBRR> <bCode>BB</bCode> <bName>BED & BREAKFAST</bName> </cOBRR> </cORRR> </cORR> ...repeat

    </getRRSR>
    </getRRS>
    </soapenv:Body>
    </soapenv:Envelope>

    I've copied these data into an XML file testdata.xml and I have tried to use the following code to replace the ampersand, but it still fails

    var dataInput = System.Text.RegularExpressions.Regex.Replace(@"testdata.xml", "&(?!amp;)", "&amp;");
    
    XmlDocument xmlDoc = new XmlDocument();
    //xmlDoc.LoadXml(getRooms);  // use this for the LIVE feed
    
    xmlDoc.Load(dataInput);
    NB: I've used xmlDoc.Load to load the test doc, as otherwise, I get ( System.Xml.XmlException: 'Data at the root level is invalid. Line 1, position 1.')

    When I run the code, I get

    System.Xml.XmlException: 'An error occurred while parsing EntityName. Line 27, position 17.'

    Line 27, position 17 is the ampersand

    <bName>BED & BREAKFAST</bName>

    I get the same error is I just use

    xmlDoc.Load(@"testdata.xml");

      so, it's like it doesn't even go through the file.

    Please, can someone show me what I am doing incorrectly ?

    thank you

    Monday, February 26, 2018 9:09 AM

Answers

  • Hi G-Oker,

    You need to replace the xml content instead of the file name.

    I suggest you try code below:

                string content = File.ReadAllText(@"D:\Test.xml");
                var dataInput = System.Text.RegularExpressions.Regex.Replace(content, "&(?!amp;)", "&amp;");
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(dataInput);
    

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by G-Oker Tuesday, February 27, 2018 9:30 AM
    Tuesday, February 27, 2018 1:46 AM

All replies

  • "I'm trying to write a c# program that will read the (soap) results given from a webservice."

    .NET already does this for you. If you are calling a web service then you'll likely be using an endpoint defined in servicemodel. To get that hooked up you'll likely be using a service reference. The service reference already knows how to properly parse a SOAP message. You don't need to do any of this yourself and, most likely, you won't do it right anyway. SOAP is a complex format where namespaces matter. Getting it wrong could result in reading the wrong data.

    Can you clarify why you aren't using a service reference (or equivalent) to call the service and have the base proxy handle the response?


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, February 26, 2018 3:40 PM
    Moderator
  • https://www.techrepublic.com/article/beware-of-the-ampersand-when-using-xml/
    Tuesday, February 27, 2018 1:03 AM
  • Hi G-Oker,

    You need to replace the xml content instead of the file name.

    I suggest you try code below:

                string content = File.ReadAllText(@"D:\Test.xml");
                var dataInput = System.Text.RegularExpressions.Regex.Replace(content, "&(?!amp;)", "&amp;");
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(dataInput);
    

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by G-Oker Tuesday, February 27, 2018 9:30 AM
    Tuesday, February 27, 2018 1:46 AM
  • Thank you Toa, this works PERFECTLY.
    Tuesday, February 27, 2018 9:30 AM
  • Hi  CooDadTx,

    When I am using the service, I am using the service reference, but, at the moment, they is a problem with my credential on their test environment, so I am using an XML file created from the documentation sent over to me, which includes test responses (which have ampersands in them).

    Rather than wait for them to fix my login, I am trying to be a bit proactive with my time :-) 

    Thank you for the clarification of the of the process though. this was very useful for me.

    Tuesday, February 27, 2018 9:34 AM