-1

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.

Community
  • 1
  • 1
Eran Meir
  • 923
  • 3
  • 17
  • 32
  • Well, your root element is "cXML" and not the "Response", so what do you expect? – Eugene Podskal Mar 07 '16 at 12:51
  • And PResponse should be Response to match the tag name. – jdweng Mar 07 '16 at 12:55
  • i am expecting "What i want to achieve:" in the question. i now changed "Response" to "cXML" as i thought it will solve the problem, but it didnt – Eran Meir Mar 07 '16 at 12:55
  • @EranMeir The Model type you use to parse the XML should fully correspond to the xml schema. So you should have at least three classes - one for cXML, one for Response and one for Status. And they shall obviously have appropriate names, types and attributes. You should read some XML deserialization tutorials. – Eugene Podskal Mar 07 '16 at 12:59
  • i also tried to changed it to "Response" but it didnt help – Eran Meir Mar 07 '16 at 12:59
  • @EugenePodskal - i used [this](http://xmltocsharp.azurewebsites.net/) site to create the right structure classes, but i am still getting an error `{"There is an error in XML document (0, 0)."}` – Eran Meir Mar 07 '16 at 13:05
  • Perhaps doctype or some schema issues causes it - try http://stackoverflow.com/questions/4203540/generate-c-sharp-class-from-xml. Or just do it yourself - it has a tiny schema. – Eugene Podskal Mar 07 '16 at 13:08
  • @EugenePodskal i updated the XML structure – Eran Meir Mar 07 '16 at 13:10
  • Please keep it to [one question](http://stackoverflow.com/questions/35841305/httpwebresponse-parse-to-xml), I have updated it with the XML structure and code. – CodeCaster Mar 07 '16 at 13:12
  • @CodeCaster - so you do realize it is not related to the answers you posted as duplicate.. – Eran Meir Mar 07 '16 at 13:27
  • @Eran you refused to update your existing question and posted a new one, that's not how it works. – CodeCaster Mar 07 '16 at 13:45
  • I can also delete the old one.. – Eran Meir Mar 07 '16 at 13:54
  • @EranMeir Are you sure that your response is what you show to us? Perhaps it uses another encoding? What is the **exact error message** you get, because provided XML can be parsed into the given data models? – Eugene Podskal Mar 07 '16 at 14:48
  • I updated the question with what caused the problem- " t = ms.ReadToEnd();" – Eran Meir Mar 07 '16 at 14:50

1 Answers1

-2

You can try this approach:

Deserialize your xml:

XmlSerializer serializer = new XmlSerializer(typeof(cXML));

StreamReader reader = new StreamReader(path);
var obj = (cXML)serializer.Deserialize(reader);
reader.Close();

Object definition:

[Serializable]
[XmlRoot("cXML")]
public class cXML
{
    [XmlElement("Response")]
    public PResponse PResponse { get; set; } 
}


[Serializable]
public class Status
{
    [XmlAttribute("code")]
    public string Code { get; set; }

    [XmlAttribute("text")]
    public string Text { get; set; }
}

[Serializable]
public class PResponse
{
    [XmlElement("Status")]
    public Status Status { get; set; }

    [XmlElement("JobID")]
    public string PlanJobID { get; set; }
}
gos
  • 390
  • 2
  • 7
  • Did you read the question, or did you dump the XML into a class generator? – CodeCaster Mar 07 '16 at 13:22
  • @CodeCaster, yes I read the question and this model will satisfy the needs of topic starter. He will be able to access "code" property by calling obj.PResponse.Status.Code from my code above – gos Mar 07 '16 at 13:33