Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C# deserialization mixed element and attribute

781 views
Skip to first unread message

Bob

unread,
Apr 23, 2009, 10:21:16 PM4/23/09
to
Hi,

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

Martin Honnen

unread,
Apr 24, 2009, 5:48:25 AM4/24/09
to
Bob wrote:

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

Bob

unread,
Apr 26, 2009, 8:18:56 PM4/26/09
to
Thank you Martin, it works for me!

Have a nice day!

sebi

unread,
Jun 17, 2009, 9:06:04 AM6/17/09
to
great, folks. this just helped me :D
0 new messages