Reputation: 55
I've been trying to use the data from an API but I have not been able to read the XML Response from it.
It cames in the form:
<?xml version="1.0" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAPSDK1="http://www.w3.org/2001/XMLSchema" xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAPSDK3="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAPSDK4:GetStoreProductsResponse xmlns:SOAPSDK4="http://www.externalwebservice.com/message/">
<StoreProducts>
<StoreID></StoreID>
<Products></Products>
</StoreProducts>
</SOAPSDK4:GetStoreProductsResponse></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And what I need is what is inside Products (for now).
I was trying to use Using C# to parse a SOAP Response (and others to not flood this) without results.
My code:
XDocument tst = XDocument.Load("Response.xml");
XNamespace xmlns = "http://schemas.xmlsoap.org/soap/envelope/";
var tstr = from result in tst.Descendants(xmlns + "StoreProducts") select result.Element("Products").Value;
I am almost sure that I am missing something basic.
Any clue will be really appreciated.
Thank you.
Upvotes: 2
Views: 19891
Reputation: 1065
In order to get XML response ( you may skip this step )
string strXml;
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream())) {
strXml = rd.ReadToEnd();
}
In order to extract Products field from the response string (strXml)
Regex regex = new Regex("<Products>(.*?)</Products>");
var regMatch = regex.Match(strXml);
string strProductValue = regMatch.Groups[1].ToString();
Upvotes: 0
Reputation: 9034
In my case I need it to read the xml sent in the post request
// read the raw request
Request.InputStream.Seek(0, SeekOrigin.Begin);
string xmlPayload = new StreamReader(Request.InputStream).ReadToEnd();
XDocument doc = XDocument.Parse(xmlPayload);
XNamespace xmlns = "urn:sobject.enterprise.soap.sforce.com";
item.sfId = doc.Descendants(xmlns + "Id").First().Value;
item.AccountId = doc.Descendants(xmlns + "AccountId").First().Value;
item.FirstName = doc.Descendants(xmlns + "FirstName").First().Value;
item.LastName = doc.Descendants(xmlns + "LastName").First().Value;
item.XmlPayload = xmlPayload;
Upvotes: 0
Reputation: 160982
In your XML StoreProducts
is not within an XML namespace, just do :
var tstr = from result in tst.Descendants("StoreProducts")
select result.Element("Products").Value;
The example code you gave would have been successful if the inner XML looked like this:
<SOAP-ENV:StoreProducts>
<StoreID></StoreID>
<Products></Products>
</SOAP-ENV:StoreProducts>
Upvotes: 4