Let's say I have the following code and you want to serialize to XML under the code:
class Human
{
[XmlElement("OwnedObjects")]
public List<WorldObjects> ownedObjects;
}
class WorldObjects
{
[XmlIgnore]
public string type;
[XmlAttribute(type)]
public string name;
public WorldObjects(string _type, string _name)
{
type = _type;
name = _name;
}
}
Human bob = new Human;
bob.ownedObjects = new List<WorldObjects>;
bob.ownedObjects.Add(new WorldObjects(drink, tea));
XML:
<Human>
<OwnedObjects drink="tea" />
</Human>
The string [XmlAttribute(type)]will result in an error.
Is it possible to change the attribute name by passing a string variable?
Thanks in advance.
EDIT: I have to apologize, I missed such a simple solution. Thanks for the answer. Also, thanks to Ben and dbc for the suggestion for better design.
source
share