XmlDeserialize conditional Deserialization based on Attribute Value

Aquammolo

New Member
I am trying to Deserialize the following XML:\[code\]<Test><string name="Name">Test name</string><string name="Description">Some fake description.</string></Test>\[/code\]Into the following class.\[code\][XmlRoot("Test")]public class Test{ [XmlElement("string")] public string Name; [XmlElement("string")] public string Description;}\[/code\]Using the code I am doing it with.\[code\]var xml = @"<Test><string name=""Name"">Test name</string><string name=""Description"">Some fake description.</string></Test>";XmlReader reader = new XmlTextReader(new StringReader(xml));XmlSerializer serializer = new XmlSerializer(typeof(Test));serializer.Deserialize(reader);\[/code\]When I run this I get an InvalidOperationException with the message\[quote\] There was an error reflecting type 'Test'.\[/quote\]If I comment out the Description property, it works. I can get the attribute value or the text, but I can't get just the XmlText with the element is string and the "name" Attribute has a specific value.Is this even possible without using LINQ?
 
Back
Top