How can i parse the response to XML, in order to read the status number and so(i wrote an example at the bottom)?
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
if (resp.StatusCode == HttpStatusCode.OK)
{
var Obj_response = new CXML();
var ms = new StreamReader(resp.GetResponseStream(), UTF8Encoding.UTF8);
t = ms.ReadToEnd();// <---- This line Caused the issue
XmlSerializer serializer = new XmlSerializer(typeof(CXML));
Obj_response = (CXML)serializer.Deserialize(ms);// <------ NOT WORKING
return true;
}
}
Response:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cXML.org/schemas/cXML/1.1.009/cXML.dtd">
<cXML payloadID="Web" xml:lang="en-US" timestamp="3/7/2016 5:21:43 AM">
<Response>
<Status code="200" text="OK" />
<JobID>WebOrder 69</JobID>
</Response>
</cXML>
PResponse obj:
[XmlRoot(ElementName = "Status")]
public class Status
{
[XmlAttribute(AttributeName = "code")]
public string Code { get; set; }
[XmlAttribute(AttributeName = "text")]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Response")]
public class Response
{
[XmlElement(ElementName = "Status")]
public Status Status { get; set; }
[XmlElement(ElementName = "JobID")]
public string JobID { get; set; }
}
[XmlRoot(ElementName = "cXML")]
public class CXML
{
[XmlElement(ElementName = "Response")]
public Response Response { get; set; }
[XmlAttribute(AttributeName = "payloadID")]
public string PayloadID { get; set; }
[XmlAttribute(AttributeName = "lang", Namespace = "http://www.w3.org/XML/1998/namespace")]
public string Lang { get; set; }
[XmlAttribute(AttributeName = "timestamp")]
public string Timestamp { get; set; }
}
What i want to achieve:
if( xml.Status.code == 200){
// something to happen on successful request
}
else{
// write the response text to log
}
I also tried to do this
I have updated the question according to the suggestion that the XML classes should fully correspond the XML structure.