Read Soap Message using C# - c#

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Head>
<h:talkId s:mustknow="1" xmlns:h="urn:schemas-test:testgate:hotel:2012-06">
sfasfasfasfsfsf</h:talkId>
</s:Head>
<s:Body>
<bookHotelResponse xmlns="urn:schemas-test:testgate:hotel:2012-06" xmlns:d="http://someURL" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d:bookingReference>123456</d:bookingReference>
<d:bookingStatus>successful</d:bookingStatus>
<d:price xmlns:p="moreURL">
<d:total>105</d:total>
</d:price>
</bookHotelResponse>
</s:Body>
</s:Envelope>
I am trying to read the above soap message XmlDocument using C#:
XmlDocument document = new XmlDocument();
document.LoadXml(soapmessage); //loading soap message as string
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
manager.AddNamespace("d", "http://someURL");
XmlNodeList xnList = document.SelectNodes("//bookHotelResponse", manager);
int nodes = xnList.Count;
foreach (XmlNode xn in xnList)
{
Status = xn["d:bookingStatus"].InnerText;
}
The count is always zero and it is not reading the bookingstatus values.

BookHotelResponse is in the namespace urn:schemas-test:testgate:hotel:2012-06 (the default namespace in the sample xml) so you need to provide that namespace in your queries:
XmlDocument document = new XmlDocument();
document.LoadXml(soapmessage); //loading soap message as string
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
manager.AddNamespace("d", "http://someURL");
manager.AddNamespace("bhr", "urn:schemas-test:testgate:hotel:2012-06");
XmlNodeList xnList = document.SelectNodes("//bhr:bookHotelResponse", manager);
int nodes = xnList.Count;
foreach (XmlNode xn in xnList)
{
Status = xn["d:bookingStatus"].InnerText;
}

Use LINQ2XML
To read bookingStatus,do this
XElement doc = XElement.Load("yourStream.xml");
XNamespace s = "http://schemas.xmlsoap.org/soap/envelope/";//Envelop namespace s
XNamespace bhr="urn:schemas-test:testgate:hotel:2012-06";//bookHotelResponse namespace
XNamespace d="http://someURL";//d namespace
foreach (var itm in doc.Descendants(s + "Body").Descendants(bhr+"bookHotelResponse"))
{
itm.Element(d+"bookingStatus").Value;//your bookingStatus value
}
LINQ2XML is cool though....:)

First you want to create a class to deseralize the xml values into
public class bookHotelResponse {
public int bookingReference { get; set; }
public int bookingStatus { get; set; }
}
Then you can utilize GetElementsByTagName to extract the body of the soap request and deseralize the request string into an object.
private static T DeserializeInnerSoapObject<T>(string soapResponse)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(soapResponse);
var soapBody = xmlDocument.GetElementsByTagName("soap:Body")[0];
string innerObject = soapBody.InnerXml;
XmlSerializer deserializer = new XmlSerializer(typeof(T));
using (StringReader reader = new StringReader(innerObject))
{
return (T)deserializer.Deserialize(reader);
}
}

As I understand you want to get response from soap service. If so, you don't have to do all this hard work (making call, parsing xml, selecting nodes to get the response value) by yourself... instead you need to Add Service Reference to your project and it will do all the rest work for you, including generating class, making asmx call and so on...
Read more about it here https://msdn.microsoft.com/en-us/library/bb628649.aspx
Everything you'll need to do after adding reference is to invoke a class method something like this
var latestRates = (new GateSoapClient())?.ExchangeRatesLatest();
return latestRates?.Rates;

Related

Null return on XmlDocument.SelectSingleNode through valid xpath (did try the old answer)

I've read the other question and apply the answer to my code, but it still doesn't work.
I use xml to read the csproj file to read/write something into it body.
This is my csproject file if anyone care!
My code:
static void Main(string[] args)
{
string file = #"D:\Project\svn_longnx\LearnSvnRevision\ConsoleApp1\WindowsFormsApp1\WindowsFormsApp1.csproj";
XmlDocument xdoc = new XmlDocument();
xdoc.Load(file);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("default", "http://schemas.microsoft.com/developer/msbuild/2003");
XmlElement root = xdoc.DocumentElement;
XmlNode node = root.SelectSingleNode("Project/PropertyGroup/ApplicationVersion",nsmgr);
Console.WriteLine(node.Value);
}
This dummy code failed either:
static void Main(string[] args)
{
string file = #"D:\Project\svn_longnx\LearnSvnRevision\ConsoleApp1\WindowsFormsApp1\WindowsFormsApp1.csproj";
XmlDocument xdoc = new XmlDocument();
xdoc.Load(file);
XmlElement root = xdoc.DocumentElement;
XmlNode node = root.SelectSingleNode("Project/PropertyGroup/ApplicationVersion");
Console.WriteLine(node.Value);
}
It's much easier to use XDocument and associated objects instead of the older XmlDocument bits. Also, you're probably falling down because of namespaces. Instead, try this code:
var doc = XDocument.Load(#"D:\Project\svn_longnx\Lear...\WindowsFormsApp1.csproj");
var ns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
var applicationVersionElements = doc.Element(ns + "Project")
.Descendants(ns + "PropertyGroup")
.Descendants(ns + "ApplicationVersion");
foreach (var element in applicationVersionElements)
{
Console.WriteLine(element.Name);
}
This is just one way as an example of how to get that specific element to demonstrate how easy it is to use.
Note, you may need to add using System.Xml.Linq;

Deserialize object property with StringReader vs XmlNodeReader

Why does XmlSerializer populate my object property with an XmlNode array when deserializing an empty typed element using XmlNodeReader instead of an empty string like it does when using StringReader (or XmlTextReader)?
The second assertion in the following code sample fails:
var doc = new XmlDocument();
doc.Load(new StringReader(#"
<Test xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Value xsi:type=""xsd:string"" />
</Test>"));
var ser = new XmlSerializer(typeof (Test));
var reader1 = new StringReader(doc.InnerXml);
var obj1 = (Test) ser.Deserialize(reader1);
Debug.Assert(obj1.Value is string);
var reader2 = new XmlNodeReader(doc.FirstChild);
var obj2 = (Test) ser.Deserialize(reader2);
Debug.Assert(obj2.Value is string);
public class Test
{
public object Value { get; set; }
}
I'm guessing it has something to do with the null internal NamespaceManager property but I'm not sure how to work around this mysterious limitation. How can I reliably deserialize a subset of my parsed XML document without converting it back into a string and re-parsing?
It looks like this is a very old XmlNodeReader bug that Microsoft have no intention of fixing. (Archived Microsoft Connect link here). I found a workaround on Lev Gimelfarb's blog here that adds namespaces to the reader's NameTable as prefixes are looked up.
public class ProperXmlNodeReader : XmlNodeReader
{
public ProperXmlNodeReader(XmlNode node) : base(node)
{
}
public override string LookupNamespace(string prefix)
{
return NameTable.Add(base.LookupNamespace(prefix));
}
}

Deserializing WCF Message at Server

I implemented a custom message inspector (via IDispatchMessageInspector) to intercept messages that are received on the server side of a WCF service so I can attempt to deserialize the message and apply some specific business logic. The problem I'm encountering is when I write the MessageBuffer's contents to a new MemoryStream and then try to deserialize, I get an error that says "The data at the root level is invalid. Line 1, position 1." I do know the data being passed in is valid as skipping over the inspector makes everything work fine.
Sample Code:
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
request = buffer.CreateMessage();
string msg = buffer.CreateMessage().ToString();
var dc = new DataContractSerializer(typeof(Adder));
using (var stream = new MemoryStream())
{
buffer.WriteMessage(stream);
stream.Position = 0;
//deserializing error occurs here
var c = dc.ReadObject(stream);
}
return null;
}
Here is the Adder class/interface:
[DataContract(Name = "adder", Namespace = "http://test.com")]
public class Adder
{
[DataMember(Name = "first")]
public int First { get; set; }
[DataMember(Name = "second")]
public int Second { get; set; }
}
[ServiceContract(Namespace = "http://test.com")]
public interface ITestSvc
{
[OperationContract(Name = "add")]
int Add(Adder adder);
}
Any suggestions or is there a better option for this? My main goal is to read the XML (in a deserialized object) on every WCF request that comes into my service.
The request object contains the WCF message headers as well as the payload. You'll need to strip off the headers and then you should be able to deserialize the message body.
For example, a SOAP message would have:
<?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="http://www.w3.org/2001/XMLSchema">
<soap:Header>
</soap:Header>
<soap:Body>
<!-- your payload -->
</soap:Body>
You could use XML navigation to get to the body element and then you'd deserialize that element alone.
EDIT:
Actually just stumbled on this method which I think should do the trick for you:
Message.GetReaderAtBodyContents
I just did this. Just paste all of the following code into your class, and call DeserializedResponse(). You'll need to change MyResponseObject to the name of whatever object you are trying to deserialize. Also, you will need to replace "_requestInspector.Response" with your own response xml string variable. The method GetSoapBodyInnerXml() will strip off the Soap Envelope, and only return your response xml which you wish to deserialize.
private MyResponseObject DeserializedResponse()
{
var rootAttribute = new XmlRootAttribute("MyResponseObject ");
rootAttribute.Namespace = #"http://www.company.com/MyResponseObjectNamespace";
XmlSerializer serializer = new XmlSerializer(typeof(MyResponseObject ), rootAttribute);
string responseSoapBodyInnerXml = GetSoapBodyInnerXml(_requestInspector.Response);
AddXmlDeclaration(ref responseSoapBodyInnerXml);
MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(responseSoapBodyInnerXml));
MyResponseObject resultingResponse = (MyResponseObject )serializer.Deserialize(memStream);
return resultingResponse;
}
private string GetSoapBodyInnerXml(string soapMessage)
{
XDocument xDoc = XDocument.Parse(soapMessage);
XNamespace nsSoap = #"http://schemas.xmlsoap.org/soap/envelope/";
return xDoc.Descendants(nsSoap + CONST_SoapBody).Descendants().First().ToString();
}
private void AddXmlDeclaration(ref string xmlString)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
//Create an XML declaration.
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
//Add the new node to the document.
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);
//Return updated xmlString with XML Declaration
xmlString = doc.InnerXml;
}
I ended up going the message header route, like Mike Parkhill suggested.

How Can I Retrieve All XmlEntityReference Objects In An XmlDocument Object?

I have a number of xml documents from which I want to retrieve all of the entity references and perform some updates on the documents. I am using an XmlDocument object to do the updates. What is the best way to retrieve all entity references from an XmlDocument object?
This may not be the best or most efficient solution, but in light of the fact that I did not receive any other answers, this is what I came up with:
XmlTextReader reader = new XmlTextReader(stream);
reader.EntityHandling = EntityHandling.ExpandCharEntities;
XmlDocument doc = new XmlDocument();
doc.Load(reader);
List<XmlEntityReference> entityRefs = new List<XmlEntityReference>();
RetrieveEntityRefs(doc.DocumentElement, doc, entityRefs);
private void RetrieveEntityRefs(XmlNode parentNode, XmlDocument doc, List<XmlEntityReference> entityReferences) {
foreach (XmlNode node in parentNode.ChildNodes)
{
if (node.NodeType == XmlNodeType.EntityReference) {
XmlEntityReference entityRef = node as XmlEntityReference;
entityReferences.Add(entityRef);
}
else if (node.HasChildNodes) {
RetrieveEntityRefs(node, doc, entityReferences);
}
}
}

XmlNodeReader returns {None}

I'm currently having trouble deserializing an XmlDocument from a web service call, here is my code : -
public void getTest(XmlDocument requestDoc)
{
XmlDocument results = new XmlDocument();
XmlSerializer serial = new XmlSerializer(typeof(DataRequest));
DataRequest req;
XmlNodeReader reader = new XmlNodeReader(requestDoc.DocumentElement);
req = (DataRequest)serial.Deserialize(reader);
response.write(req.toString());
}
now, the trouble I am having is that the XmlNodeReader just contains "{None}" when I step through in debug, the requestDoc definatly has the expected XML structure, any ideas?
Kind regards
Gib
The "none" probably just means it hasn't started iterating yet, and is at BOF (for want of a better term). It should still work. Usually, if it doesn't it means the namespaces are incorrect - double-check for xmlns in the source.
This works fine, for example:
public class Test
{
static void Main()
{
var doc = new XmlDocument();
doc.LoadXml(#"<Test foo=""bar""></Test>");
var ser = new XmlSerializer(typeof(Test));
using (var reader = new XmlNodeReader(doc.DocumentElement))
{
var test = (Test)ser.Deserialize(reader);
Console.WriteLine(test.Foo);
}
}
[XmlAttribute("foo")]
public string Foo { get; set; }
}

Categories