locked
How do i parse SOAP formed XML RRS feed

  • Question

  • User1536465747 posted

    How do i get only the important data without using string manipulation classes and stuff? I mean is there a built-in feature in WCF that can take care of these messages?

    e.g. 

    <?xml version='1.0' encoding='UTF-8'?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">
    <SOAP-ENV:Body>
    etc. etc.

    Thank you

    Sunday, September 6, 2015 2:16 AM

Answers

  • User-271186128 posted

    Hi KulerMaster,

    As for this issue, I suggest you could refer to the following code to parse SOAP format xml file.

                string xml = Server.MapPath("SOAPFile.xml");
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(xml);
                XmlNamespaceManager xnm = new XmlNamespaceManager(xmldoc.NameTable);
                xnm.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
                xnm.AddNamespace("Mo", "http://www.monternet.com/dsmp/schemas/");
    
    
    
    
                XmlNodeList lst = xmldoc.SelectNodes("//SOAP-ENV:Header/Mo:TransactionID", xnm);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < lst.Count; i++)
                {
                    XmlNode node = lst[i];
                    XmlElement ele = (XmlElement)node;
                    sb.AppendLine(ele.Name + ":" + ele.InnerText);
                    sb.AppendLine("<br />");
                }

    For more details, please refer to the following link: http://forums.asp.net/t/1297453.aspx?How+to+get+all+elements+in+an+XML+document

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 7, 2015 5:35 AM

All replies

  • User475983607 posted

    Yes, the common approach is to generate a client proxy using svcutil.exe or adding service reference in Visual Studio.  Please see the following tutorial on creating and consuming WCF services.

    https://msdn.microsoft.com/en-us/library/bb386386.aspx

    Sunday, September 6, 2015 7:53 AM
  • User1536465747 posted

    OK i did that but what happens when someone from outside is sending the SOAP message? I receive the request as STRING and parse AS IS. However i was wondering if there is a way to get rid of the SOAP elements ending up with a clean of SOAP xml data? 

    My method looks like the following:

    public void ReadData(String data){
    try
    {
        XDocument doc = XDocument.Parse(data);
        //etc.etc.
    

    right now it does parse the soap message but without stripping down the not needed elements like mentioned in the first post. 

    Thank you so much for any further help

    Sunday, September 6, 2015 11:59 AM
  • User475983607 posted

    SOAP serialization and deserialization is handled automatically by the WCF framework. The data structure passed between client is defined by the service contract.

    The snippet of code above indicates that the ReadData input argument, the data passed from the client to the service, is nothing more than a string.  This string is part of the contract that defines what data types the service expects when invoking ReadData().  If there are further requirements on how the string should be formatted, that's up to the application requirements and has nothing to do with SOAP serialization.

    You must understand is that at this point in the code execution where ReadData is being invoked, the SOAP message has all ready been deserialized.  

    public void ReadData(String data){
    try
    {
        XDocument doc = XDocument.Parse(data);
        //etc.etc.

    The "data" argument is simply a string that was once contained in the SOAP message.  The string itself can be formatted as many ways as a string can be formatted.  It is up to the program logic to convert the string to the proper type according the application requirements.  IMHO, it is much easier to pass an actual type rather than an string formatted as XML but, in the end, it depends on what you're trying to do.

    It may be beneficial for you to run through a few more tutorials so that you get a better idea of how WCF works.

    https://msdn.microsoft.com/en-us/library/cc668184.aspx?f=255&MSPPError=-2147217396

    You might also consider posting all your code so we can see what you're doing.

    Sunday, September 6, 2015 2:14 PM
  • User-271186128 posted

    Hi KulerMaster,

    As for this issue, I suggest you could refer to the following code to parse SOAP format xml file.

                string xml = Server.MapPath("SOAPFile.xml");
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(xml);
                XmlNamespaceManager xnm = new XmlNamespaceManager(xmldoc.NameTable);
                xnm.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
                xnm.AddNamespace("Mo", "http://www.monternet.com/dsmp/schemas/");
    
    
    
    
                XmlNodeList lst = xmldoc.SelectNodes("//SOAP-ENV:Header/Mo:TransactionID", xnm);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < lst.Count; i++)
                {
                    XmlNode node = lst[i];
                    XmlElement ele = (XmlElement)node;
                    sb.AppendLine(ele.Name + ":" + ele.InnerText);
                    sb.AppendLine("<br />");
                }

    For more details, please refer to the following link: http://forums.asp.net/t/1297453.aspx?How+to+get+all+elements+in+an+XML+document

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 7, 2015 5:35 AM