The XmlSerializer does everything I want with one exception. I need to pair an element with another element as an attribute of that element. I don't want to write a completely custom serialize method. Here's my class:
public class Transaction { [XmlElement("ID")] public int m_id;
[XmlElement("TransactionType")]
public string m_transactiontype;
[XmlAttribute("TransactionTypeCode")]
public string m_transactiontypecode;
}
I instantiate and serialize as follows;
Transaction tx = new Transaction();
tx.m_id = 1;
tx.m_transactiontype = "Withdrawal";
tx.m_transactiontypecode = "520";
StringWriter o = new
StringWriter(CultureInfo.InvariantCulture);
XmlSerializer s = new
XmlSerializer(typeof(Transaction));
s.Serialize(o, tx);
Console.Write(o.ToString());
Gives me:
<Transaction TransactionTypeCode="520">
<ID>1</ID>
<TransactionType>Withdrawal</TransactionType>
</Transaction>
I want:
<Transaction>
<ID>1</ID>
<TransactionType TransactionTypeCode="520">Withdrawal</TransactionType>
</Transaction>
Someone (Chris Dogget) suggested:
public class Transaction
{
[XmlElement("ID")]
public int m_id;
public TransactionType m_transactiontype;
}
public class TransactionType
{
public TransactionType(){}
public TransactionType(string type) { this.m_transactiontype = type; }
[XmlTextAttribute]
public string m_transactiontype;
[XmlAttribute("TransactionTypeCode")]
public string m_transactiontypecode;
}
The use of the TransactionType class looks promising - can you show me how you would instantiate the classes before serializing?
Thanks!