Hello! It is necessary to deserialize xml of the simplest type:

<?xml version="1.0" encoding="UTF-8"?> <requestID>057f4c78-a022</requestID> 

The following model model returns an empty result:

 [Serializable] [XmlRoot("requestID")] public class RequestId { public string RequestID { get; set; } RequestId() { } } 

I understand that the model is incorrect, but I don’t understand how to correctly describe the contents of the requestID node.

I deserialize so:

 XmlSerializer deserializer = new XmlSerializer(typeof(RequestId)); StringReader xmlString = new StringReader(responseString); RequestId orderStatus = (RequestId)deserializer.Deserialize(xmlString); 
  • Well, so you show me how you serialize / deserialize, what problems do you have with this, what doesn't work? - tym32167
  • The problem is not in deserialization, but in the correctness of building the model. I deserialize like this: XmlSerializer deserializer = new XmlSerializer(typeof(RequestId)); StringReader xmlString = new StringReader(responseString); RequestId orderStatus = (RequestId)deserializer.Deserialize(xmlString); XmlSerializer deserializer = new XmlSerializer(typeof(RequestId)); StringReader xmlString = new StringReader(responseString); RequestId orderStatus = (RequestId)deserializer.Deserialize(xmlString); where responseString is the above xml - andrey.t
  • 2
    An empty constructor with no parameters can be omitted when declaring a class, the compiler by default creates such a constructor if it is not implemented. - user218976
  • @Anamnian, but not private, probably? - Andrew NOP
  • one
    @Anamnian This is a requirement when serializing xml - Monomax

1 answer 1

This class will suit you:

 [XmlRoot(ElementName = "requestID")] public class RequestID { [XmlText] public string Text { get; set; } } 

The fact is that by default all properties / fields of a class are represented as XmlElement , if you need something different, you must specify this explicitly.