T
Todd
I'm developing an application that accepts XML input from a diverse set of
clients. Some clients specify values as an attribute, and some specify values
as a sub-element.
The XmlSerializer doesn't seem to handle both cases (attribute vs. element)
by default. Is there a way to get the Deserializer to recognizer EITHER
attribute or element for the same class?
If I add the [XmlAttribute] attribute to the member sex (below), then the
attributed deserialization works, but the sub-element version then fails. It
seems there's no way to get the Deserializer to handle both cases?
According to the standard XML spec, specifying a value as either an element
or an attributes should be identical.
[Serializable]
public class person
{
public string sex;
public string firstname;
public string lastname;
}
public void Test()
{
string textWithElement =
@"<person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>";
string textWithAttribute =
@"<person sex=""female"">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>";
TestDeserialization(textWithElement);
TestDeserialization(textWithAttribute);
// Output
// person, sex=female, firstname=Anna, lastname=Smith
// person, sex=, firstname=Anna, lastname=Smith
}
public void TestDeserialization(string text)
{
person p;
XmlSerializer mySerializer = new XmlSerializer(typeof(person));
MemoryStream mem = new MemoryStream(UTF8Encoding.UTF8.GetBytes(text));
object o = mySerializer.Deserialize(mem);
p = (person)o;
System.Diagnostics.Debug.WriteLine(string.Format("person, sex={0},
firstname={1}, lastname={2}", p.sex, p.firstname, p.lastname));
}
clients. Some clients specify values as an attribute, and some specify values
as a sub-element.
The XmlSerializer doesn't seem to handle both cases (attribute vs. element)
by default. Is there a way to get the Deserializer to recognizer EITHER
attribute or element for the same class?
If I add the [XmlAttribute] attribute to the member sex (below), then the
attributed deserialization works, but the sub-element version then fails. It
seems there's no way to get the Deserializer to handle both cases?
According to the standard XML spec, specifying a value as either an element
or an attributes should be identical.
[Serializable]
public class person
{
public string sex;
public string firstname;
public string lastname;
}
public void Test()
{
string textWithElement =
@"<person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>";
string textWithAttribute =
@"<person sex=""female"">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>";
TestDeserialization(textWithElement);
TestDeserialization(textWithAttribute);
// Output
// person, sex=female, firstname=Anna, lastname=Smith
// person, sex=, firstname=Anna, lastname=Smith
}
public void TestDeserialization(string text)
{
person p;
XmlSerializer mySerializer = new XmlSerializer(typeof(person));
MemoryStream mem = new MemoryStream(UTF8Encoding.UTF8.GetBytes(text));
object o = mySerializer.Deserialize(mem);
p = (person)o;
System.Diagnostics.Debug.WriteLine(string.Format("person, sex={0},
firstname={1}, lastname={2}", p.sex, p.firstname, p.lastname));
}