I need to process some XML input which has HTML code in some tags. For these tags I want the raw content to process it later. I followed this answer and used XmlElement
which works fine in most cases. The only problem I'm facing are self closing tags.
[Serializable]
public class Root
{
public XmlElement Description { get; set; }
public string Name { get; set; }
}
var serializer = new XmlSerializer(typeof(Root));
var obj1 = serializer.Deserialize(new StringReader(@"<Root><Description><p>test</p></Description><Name>Test</Name></Root>"));
// Description: "Element, Name=\"p\""
// Name: "Test"
var obj2 = serializer.Deserialize(new StringReader(@"<Root><Description></Description><Name>Test</Name></Root>"));
// Description: null
// Name: "Test"
var obj3 = serializer.Deserialize(new StringReader(@"<Root><Description/><Name>Test</Name></Root>"));
// Description: "Element, Name=\"Name\""
// Name: null
obj1
and obj2
are ok (obj2.Description == ""
would be better) but in obj3
the Description
member is greedy and contains the Name
part.
Is there a workaround for this problem?