Help Needed in XML Parsing

  • Thread starter Thread starter Born2Achieve
  • Start date Start date
B

Born2Achieve

Guest
Hi,

I am trying to call web service with no proxy and i am able to achieve it. below the wrapper am using to call the service.

public static class SOAPHelper
{
/// <summary>
/// Sends a custom sync SOAP request to given URL and receive a request
/// </summary>
/// <param name="url">The WebService endpoint URL</param>
/// <param name="action">The WebService action name</param>
/// <param name="parameters">A dictionary containing the parameters in a key-value fashion</param>
/// <param name="soapAction">The SOAPAction value, as specified in the Web Service's WSDL (or NULL to use the url parameter)</param>
/// <param name="useSOAP12">Set this to TRUE to use the SOAP v1.2 protocol, FALSE to use the SOAP v1.1 (default)</param>
/// <returns>A string containing the raw Web Service response</returns>
public static string SendSOAPRequest(string url, string action, Dictionary<string, string> parameters, string soapAction = null, bool useSOAP12 = false)
{
// Create the SOAP envelope
XmlDocument soapEnvelopeXml = new XmlDocument();
var xmlStr = (useSOAP12)
? @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""XML Schema""
xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">
<soap12:Body>
<{0} xmlns=""{1}"">{2}</{0}>
</soap12:Body>
</soap12:Envelope>"
: @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""XML Schema"">
<soap:Body>
<{0} xmlns=""{1}"">{2}</{0}>
</soap:Body>
</soap:Envelope>";
string parms = string.Join(string.Empty, parameters.Select(kv => String.Format("<{0}>{1}</{0}>", kv.Key, kv.Value)).ToArray());
var s = String.Format(xmlStr, action, new Uri(url).GetLeftPart(UriPartial.Authority) + "/", parms);
soapEnvelopeXml.LoadXml(s);

// Create the web request
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", soapAction ?? url);
webRequest.ContentType = (useSOAP12) ? "application/soap+xml;charset=\"utf-8\"" : "text/xml;charset=\"utf-8\"";
webRequest.Accept = (useSOAP12) ? "application/soap+xml" : "text/xml";
webRequest.Method = "POST";

// Insert SOAP envelope
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}

// Send request and retrieve result
string result;
using (WebResponse response = webRequest.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
result = rd.ReadToEnd();
}
}
return result;
}
}

SOAP data (actual) what i receive after calling the service :

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="XML Schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:processResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="https://www.hcsprovider.com/">
<processReturn href="#id0"/>
</ns1:processResponse>
<multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:TCBalanceInquiryResponse" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:BeanService">
<cardNumber xsi:type="xsd:string">5327970100637437</cardNumber>
<employerID xsi:type="xsd:string"></employerID>
<members href="#id1"/>
<organizationNumber xsi:type="xsd:string">T40</organizationNumber>
<purseNumber xsi:type="xsd:string">10630560</purseNumber>
<purseType xsi:type="xsd:string">ALL</purseType>
<responseCode xsi:type="xsd:string"></responseCode>
<responseMessage xsi:type="xsd:string"></responseMessage>
<resultArtifacts xsi:type="soapenc:Array" soapenc:arrayType="xsd:string[4]" xmlns:ns3="http://www.w3.org/2002/12/soap-encoding">
<item>[TCBalanceInquiryDto][authorizedBalance]$ .00[/authorizedBalance][description][/description][ledgerBalance]$ 20.00[/ledgerBalance][purseNumber]10630560[/purseNumber][purseType]PRK[/purseType][status]AVAILABLE[/status][/TCBalanceInquiryDto]</item>
<item>[TCBalanceInquiryDto][authorizedBalance]$ .00[/authorizedBalance][description][/description][ledgerBalance][/ledgerBalance][purseNumber]10630560[/purseNumber][purseType]PTX[/purseType][status]NOT FOUND[/status][/TCBalanceInquiryDto]</item>
<item>[TCBalanceInquiryDto][authorizedBalance]$ .00[/authorizedBalance][description][/description][ledgerBalance][/ledgerBalance][purseNumber]10630560[/purseNumber][purseType]TRN[/purseType][status]NOT FOUND[/status][/TCBalanceInquiryDto]</item>
<item>[TCBalanceInquiryDto][authorizedBalance]$ .00[/authorizedBalance][description][/description][ledgerBalance][/ledgerBalance][purseNumber]10630560[/purseNumber][purseType]TTX[/purseType][status]NOT FOUND[/status][/TCBalanceInquiryDto]</item>
</resultArtifacts>
<soapFault xsi:type="xsd:string"></soapFault>
</multiRef>
<multiRef id="id1" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soapenc:Array" soapenc:arrayType="xsd:anyType[4]" xmlns:ns4="http://www.w3.org/2002/12/soap-encoding" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<item href="#id2"/>
<item href="#id3"/>
<item href="#id4"/>
<item href="#id5"/>
</multiRef>
<multiRef id="id2" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns5:TCBalanceInquiryDto" xmlns:ns5="urn:BeanService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<authorizedBalance xsi:type="xsd:string">$ .00</authorizedBalance>
<description xsi:type="xsd:string"></description>
<ledgerBalance xsi:type="xsd:string">$ 20.00</ledgerBalance>
<purseNumber xsi:type="xsd:string">10630560</purseNumber>
<purseType xsi:type="xsd:string">PRK</purseType>
<status xsi:type="xsd:string">AVAILABLE</status>
</multiRef>
<multiRef id="id4" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns6:TCBalanceInquiryDto" xmlns:ns6="urn:BeanService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<authorizedBalance xsi:type="xsd:string">$ .00</authorizedBalance>
<description xsi:type="xsd:string"></description>
<ledgerBalance xsi:type="xsd:string"></ledgerBalance>
<purseNumber xsi:type="xsd:string">10630560</purseNumber>
<purseType xsi:type="xsd:string">TRN</purseType>
<status xsi:type="xsd:string">NOT FOUND</status>
</multiRef>
<multiRef id="id3" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns7:TCBalanceInquiryDto" xmlns:ns7="urn:BeanService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<authorizedBalance xsi:type="xsd:string">$ .00</authorizedBalance>
<description xsi:type="xsd:string"></description>
<ledgerBalance xsi:type="xsd:string"></ledgerBalance>
<purseNumber xsi:type="xsd:string">10630560</purseNumber>
<purseType xsi:type="xsd:string">PTX</purseType>
<status xsi:type="xsd:string">NOT FOUND</status>
</multiRef>
<multiRef id="id5" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns8:TCBalanceInquiryDto" xmlns:ns8="urn:BeanService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<authorizedBalance xsi:type="xsd:string">$ .00</authorizedBalance>
<description xsi:type="xsd:string"></description>
<ledgerBalance xsi:type="xsd:string"></ledgerBalance>
<purseNumber xsi:type="xsd:string">10630560</purseNumber>
<purseType xsi:type="xsd:string">TTX</purseType>
<status xsi:type="xsd:string">NOT FOUND</status>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>

i copied this xml response and created a class and pasted as special "PasteXMLasClasses" feature of VS IDE. Below the code after i did that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service
{

// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{

private EnvelopeBody bodyField;

/// <remarks/>
public EnvelopeBody Body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{

private processResponse processResponseField;

private multiRef[] multiRefField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "https://www.hcsprovider.com/")]
public processResponse processResponse
{
get
{
return this.processResponseField;
}
set
{
this.processResponseField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("multiRef", Namespace = "")]
public multiRef[] multiRef
{
get
{
return this.multiRefField;
}
set
{
this.multiRefField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "https://www.hcsprovider.com/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "https://www.hcsprovider.com/", IsNullable = false)]
public partial class processResponse
{

private processReturn processReturnField;

private string encodingStyleField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
public processReturn processReturn
{
get
{
return this.processReturnField;
}
set
{
this.processReturnField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public string encodingStyle
{
get
{
return this.encodingStyleField;
}
set
{
this.encodingStyleField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class processReturn
{

private string hrefField;

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string href
{
get
{
return this.hrefField;
}
set
{
this.hrefField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class multiRef
{

private string authorizedBalanceField;

private object descriptionField;

private string ledgerBalanceField;

private multiRefItem[] itemField;

private ulong cardNumberField;

private bool cardNumberFieldSpecified;

private object employerIDField;

private multiRefMembers membersField;

private string organizationNumberField;

private uint purseNumberField;

private bool purseNumberFieldSpecified;

private string purseTypeField;

private string statusField;

private object responseCodeField;

private object responseMessageField;

private multiRefResultArtifacts resultArtifactsField;

private object soapFaultField;

private string idField;

private byte rootField;

private string encodingStyleField;

private string arrayTypeField;

/// <remarks/>
public string authorizedBalance
{
get
{
return this.authorizedBalanceField;
}
set
{
this.authorizedBalanceField = value;
}
}

/// <remarks/>
public object description
{
get
{
return this.descriptionField;
}
set
{
this.descriptionField = value;
}
}

/// <remarks/>
public string ledgerBalance
{
get
{
return this.ledgerBalanceField;
}
set
{
this.ledgerBalanceField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("item")]
public multiRefItem[] item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}

/// <remarks/>
public ulong cardNumber
{
get
{
return this.cardNumberField;
}
set
{
this.cardNumberField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool cardNumberSpecified
{
get
{
return this.cardNumberFieldSpecified;
}
set
{
this.cardNumberFieldSpecified = value;
}
}

/// <remarks/>
public object employerID
{
get
{
return this.employerIDField;
}
set
{
this.employerIDField = value;
}
}

/// <remarks/>
public multiRefMembers members
{
get
{
return this.membersField;
}
set
{
this.membersField = value;
}
}

/// <remarks/>
public string organizationNumber
{
get
{
return this.organizationNumberField;
}
set
{
this.organizationNumberField = value;
}
}

/// <remarks/>
public uint purseNumber
{
get
{
return this.purseNumberField;
}
set
{
this.purseNumberField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool purseNumberSpecified
{
get
{
return this.purseNumberFieldSpecified;
}
set
{
this.purseNumberFieldSpecified = value;
}
}

/// <remarks/>
public string purseType
{
get
{
return this.purseTypeField;
}
set
{
this.purseTypeField = value;
}
}

/// <remarks/>
public string status
{
get
{
return this.statusField;
}
set
{
this.statusField = value;
}
}

/// <remarks/>
public object responseCode
{
get
{
return this.responseCodeField;
}
set
{
this.responseCodeField = value;
}
}

/// <remarks/>
public object responseMessage
{
get
{
return this.responseMessageField;
}
set
{
this.responseMessageField = value;
}
}

/// <remarks/>
public multiRefResultArtifacts resultArtifacts
{
get
{
return this.resultArtifactsField;
}
set
{
this.resultArtifactsField = value;
}
}

/// <remarks/>
public object soapFault
{
get
{
return this.soapFaultField;
}
set
{
this.soapFaultField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
public byte root
{
get
{
return this.rootField;
}
set
{
this.rootField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public string encodingStyle
{
get
{
return this.encodingStyleField;
}
set
{
this.encodingStyleField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
public string arrayType
{
get
{
return this.arrayTypeField;
}
set
{
this.arrayTypeField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class multiRefItem
{

private string hrefField;

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string href
{
get
{
return this.hrefField;
}
set
{
this.hrefField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class multiRefMembers
{

private string hrefField;

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string href
{
get
{
return this.hrefField;
}
set
{
this.hrefField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class multiRefResultArtifacts
{

private string[] itemField;

private string arrayTypeField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("item")]
public string[] item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
public string arrayType
{
get
{
return this.arrayTypeField;
}
set
{
this.arrayTypeField = value;
}
}
}
}

Finally, i am trying to deserialize as below


var str = SOAPHelper.SendSOAPRequest(URL, "process", parameters);

var rawXML = XDocument.Parse(str);
Service.Envelope deserializedObject;
using (var reader = rawXML.CreateReader(System.Xml.Linq.ReaderOptions.None))
{
var ser = new XmlSerializer(typeof(Service.Envelope));
deserializedObject = (Service.Envelope)ser.Deserialize(reader);
}


I am getting error as

{"There is an error in XML document (0, 0)."}


I am not sure where the error is coming from. please help me solve this issue. i spent lot of time to see whats wrong in the xml file. my bad, i couldn't find it. nay help will be highly appreciated.

Thank you.






loving dotnet

Continue reading...
 
Back
Top