Reputation: 4731
I was trying to read this SOAP XML Message
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<EchoSoapRequest xmlns="http://tempuri.org/">
<grantapplication externalsystemreference="0743A61C-B3F8-4B51-AF1E-FBE76172D34C" externalid="d77ddae7-ad19-4c4a-b3bf-1e83df82e40f">
<scheme>CDD</scheme>
<applicationdate> 20170126 </applicationdate>
<category> CDD F </category>
<applicant>
<title>Mr</title>
</applicant>
</grantapplication>
</EchoSoapRequest>
</soap:Body>
</soap:Envelope>
This is my approch
public bool SaveContacts(XmlDocument application)
{
XDocument xmessage = XDocument.Parse(application.OuterXml);
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";//Envelop namespace s
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";//Envelop namespace s
XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";//Envelop namespace s
XNamespace d = "http://tempuri.org/";//bookHotelResponse namespace
XNamespace externalsystemreference = "0743A61C-B3F8-4B51-AF1E-FBE76172D34C";//d namespace
XNamespace externalid = "d77ddae7-ad19-4c4a-b3bf-1e83df82e40f";//d namespace
foreach (var itm in xmessage.Descendants(xsi + "Body")
.Descendants(externalsystemreference + "grantapplication").Descendants(externalid + "grantapplication"))
{
string ss = itm.Element(d + "scheme").Value;
}
return true;
}
But still not picking any value for ss can someone see anything wrong with this
Upvotes: 0
Views: 69
Reputation: 211
If we assume that you maintain the correct xml (as per the comments above), and that your method is truly receiving a valid "XmlDocument"; then:
private static XmlDocument HisXml()
{
var xDoc = XDocument.Load("C:\\temp\\HisXml.xml");
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xDoc.ToString());
return xmlDocument;
}
Then, this works, word of caution, I would address possible NULLs and such, but you can handle that how you want :) : (Note, no need to do a loop through the "scheme" nodes if you KNOW you are only receiving one, you need figure that out
public static bool SaveContacts(XmlDocument application)
{
// COMMENTED CODE IS YOU OLD STUFF
//XDocument xmessage = XDocument.Parse(application.OuterXml);
//XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";//Envelop namespace s
//XNamespace xsd = "http://www.w3.org/2001/XMLSchema";//Envelop namespace s
//XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";//Envelop namespace s
//XNamespace d = "http://tempuri.org/";//bookHotelResponse namespace
//XNamespace externalsystemreference = "0743A61C-B3F8-4B51-AF1E-FBE76172D34C";//d namespace
//XNamespace externalid = "d77ddae7-ad19-4c4a-b3bf-1e83df82e40f";//d namespace
XmlNodeList nodeList = application.GetElementsByTagName("scheme");
string hisStuff;
foreach (XmlNode n in nodeList)
{
hisStuff = n.InnerText;
}
//foreach (var itm in xmessage.Descendants(xsi + "Body")
// .Descendants(externalsystemreference + "grantapplication").Descendants(externalid + "grantapplication"))
//{
// string ss = itm.Element(d + "scheme").Value;
//}
return true;
}
Upvotes: 1