2016-03-07 60 views
-1

如何解析對XML的響應,以便讀取狀態編號等(我在底部寫了一個示例)?將StreamReader響應轉換爲xml

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

響應:

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

我想達到的目標:

if(xml.Status.code == 200){ 
// something to happen on successful request 
} 
else{ 
// write the response text to log 
} 

我也試着做this


我已經根據XML類應完全對應XML結構的建議更新了問題。

+0

那麼,你的根元素是 「cXML的」,而不是 「響應」,所以你期望什麼? –

+0

而PResponse應該是響應以匹配標籤名稱。 – jdweng

+0

我在期待「我想達到什麼:」這個問題。 我現在將「Response」更改爲「cXML」,因爲我認爲它會解決問題,但它沒有 –

回答

-2

你可以試試這個方法:

反序列化的XML:

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

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

對象定義:

[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; } 
} 
+0

您是否閱讀過這個問題,或者您是否將XML轉儲到類生成器中? – CodeCaster

+0

@CodeCaster,是的,我讀了這個問題,這個模型將滿足主題啓動者的需求。他將能夠通過從我的代碼中調用obj.PResponse.Status.Code來訪問「代碼」屬性 –