0

I'm using this code to perform an HTTP request and parse the XML response:

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;
    }
}

It shows:

Root element is missing.

The XML looks like this:

<?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>

And the generated classes look like this:

[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 do is something like that:

if(Obj_response.Status.code == 200)
{
    // something to happen on successful request
}
else
{
    // write the response text to log
}
Eran Meir
  • 923
  • 3
  • 17
  • 32
  • You're trying to "read an XML attribute", which is explained in plenty of questions, http://stackoverflow.com/questions/933687/read-xml-attribute-using-xmldocument, http://stackoverflow.com/questions/3750678/getting-attribute-value-of-an-xml-document-using-c-sharp, http://stackoverflow.com/questions/18017692/c-sharp-get-values-from-xml-attributes – CodeCaster Mar 07 '16 at 10:31
  • They all talks about a file doc. i am talking about `GetResponseStream()` to xml – Eran Meir Mar 07 '16 at 10:34
  • Yeah so [Populate XDocument from String](http://stackoverflow.com/questions/747554/populate-xdocument-from-string), [XmlDocument - load from string?](http://stackoverflow.com/questions/4929653/xmldocument-load-from-string). – CodeCaster Mar 07 '16 at 10:34
  • No, don't find a solution.. theey all don't talk about a stream response, i did find some [post](http://stackoverflow.com/questions/4271618/how-to-convert-streamreader-data-to-xmldocument) that suppose to work, but it don't work for me, i am getting an error "{"Root element is missing."}" at this line `document.Load(stream);` – Eran Meir Mar 07 '16 at 10:44
  • Then please edit your attempts, including the error and your research, into your question. – CodeCaster Mar 07 '16 at 10:47
  • I have edited the question - please remove the "duplicate" .. it's not the same as the answer you suggested – Eran Meir Mar 07 '16 at 10:53
  • Well it is, just now you're adding the information that you tried to load the XML but couldn't. The end goal and the way to get there don't change. – CodeCaster Mar 07 '16 at 11:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105552/discussion-between-eran-meir-and-codecaster). – Eran Meir Mar 07 '16 at 11:32
  • still didn't manage to do it.. help:) ? – Eran Meir Mar 07 '16 at 11:45

1 Answers1

0

eventually i found the problem, the problem was with this line t = ms.ReadToEnd(); that cause the stream to lap through the end cause nothing to be after that.

Eran Meir
  • 923
  • 3
  • 17
  • 32