I tried to deserialize the following XML file but failed to get the
"Status" value back - always gets null value.
The XML file:
<cXML timestamp="2001-10-31T23:07:22-08:00"
payloadID="1004598442900-83...@10.10.13.100">
<Response>
<Status code="500" text="Bad Response">500 - Bad Response</Status>
</Response>
</cXML>
This is the Class I created to deserialize the above XML:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
namespace GetXMLValue
{
[Serializable]
public class cXML
{
private string _timestamp;
private string _payloadID;
[XmlAttribute("timestamp")]
public string timestamp
{
get { return _timestamp; }
set { _timestamp = value; }
}
[XmlAttribute("payloadID")]
public string payloadID
{
get { return _payloadID; }
set { _payloadID = value; }
}
public Response Response;
}
[Serializable]
public class Response
{
public Status Status;
}
[Serializable]
public class Status
{
private string code;
private string text;
private string _status;
[XmlAttribute("code")]
public string Code
{
get { return code; }
set { code = value; }
}
[XmlAttribute("text")]
public string Text
{
get { return text; }
set { text = value; }
}
public string status
{
get { return _status; }
set { _status= value; }
}
}
}
My problem is I can get the "code" attribute value ("500"), and the
"text" attribute value ("Bad Request"), but I can't get the "Status"
element value ("500 - Bad Request"). I understand the class I created
must have something wrong, but I don't know how to fix it.
I tried to use XSD to generate a class for me (by using the XML file)
but the class is a bit too complex to me to implemnet, and I believe
there should be a simple solution exists.
Any suggestions/ideas are highly appreciated.
Thank you in advance.
Bob
> My problem is I can get the "code" attribute value ("500"), and the
> "text" attribute value ("Bad Request"), but I can't get the "Status"
> element value ("500 - Bad Request"). I understand the class I created
> must have something wrong, but I don't know how to fix it.
Use XmlText e.g.
public Response Response;
}
[XmlText()]
public string Stat
{
get { return _status; }
set { _status = value; }
}
}
--
Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
Have a nice day!