Groups | Blog | Home
all groups > dotnet xml > december 2005 >

dotnet xml : Deserializing primitive element with attribute



voipdealer
12/6/2005 6:39:29 PM
I think this XML response sucks, but it's being sent to us from a
third-party, so I can't do anything about it except work with it.

I am receiving the following return code elements inside a response:

<Response>
<RC1 message="SUCCESS">0</RC1>
<RC2 message="Blah blah blah">1</RC2>
</Response>

Now, if I make RC1 and RC2 "int" members of a Response class, it parses
the 0 and 1 correctly, but misses the "SUCCESS" and "Blah blah blah".
If i create a class called RC that has an [XmlAttribute] named
"message", it parses the message properly, but I have no idea how to
access the values 0 and 1 because they have no tag.

Any ideas?

--- Deserializing routine ---
XmlSerializer xs = new XmlSerializer(typeof(Response));
MemoryStream memoryStream = new
MemoryStream(StringToUTF8ByteArray(strXml));
Response r = (Response)xs.Deserialize(memoryStream);

-- RC class, loses the 0/1 value --
public class RC
{
public int ReturnCode;

[XmlAttribute]
public string message;

public RC()
{
ReturnCode = -999;
message = "NULL";
}
}

-- The other way, lose the message attribute --
public class Response
{
public int RC1;
public int RC2;
}
Chris Lovett
12/7/2005 12:38:49 AM
Add the following to your RC class:

[System.Xml.Serializerion.XmlText]
public string Value;

[quoted text, click to view]

voipdealer
12/13/2005 6:45:32 AM
Thanks, that worked.


[quoted text, click to view]
AddThis Social Bookmark Button