EDN Admin
Well-known member
Hi,
I have an xml message coming in from a 3rd party system. A typical message is of the form:
<div style="color:Black; background-color:White
<pre><span style="color:Blue <<span style="color:#A31515 Deal<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyD1<span style="color:Blue >......<span style="color:Blue </<span style="color:#A31515 PropertyD1<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyD2<span style="color:Blue >......<span style="color:Blue </<span style="color:#A31515 PropertyD2<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 etc<span style="color:Blue >......<span style="color:Blue </<span style="color:#A31515 etc<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Transaction<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyT1<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 PropertyT1<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyT2<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 PropertyT2<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 etc<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 etc<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Transaction<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Transaction<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyT1<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 PropertyT1<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyT2<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 PropertyT2<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 etc<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 etc<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Transaction<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Deal<span style="color:Blue >
[/code]
so basically we have a primary object (a deal) with a bunch of its own properties, and also with a collection of one or more secondary objects (transactions).
When this message comes into my system, the first thing I want to do with this message is to deserialize it and start passing an object around the place. So Im trying to reverse engineer a couple of classes, a Deal class and a Transaction class, which this
document will deserialize into.
Each class contains the necessary embellishments on their properties (pretty much) to generate the xml above upon serialization, but where Im having trouble is the link between the deal class and the transaction class, so that when a deal object is
serialized, I get the
<div style="color:Black; background-color:White
<pre><span style="color:Blue <<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Transaction<span style="color:Blue >
....
<span style="color:Blue </<span style="color:#A31515 Transaction<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Transactions<span style="color:Blue >
[/code]
structure coming into play.
I have tried having the deal class just having a collection of transactions (which is probably the easiest way to implement this if youre not worried about how the serialized data actually looks). In other words...
<div style="color:Black; background-color:White
<pre> <span style="color:Gray /// <span style="color:Gray <summary>
<span style="color:Gray ///<span style="color:Green The Transaction collection
<span style="color:Gray /// <span style="color:Gray </summary>
[XmlElement(ElementName = <span style="color:#A31515 "Transactions")]
<span style="color:Blue public Collection<MyTransactionClass> Transactions;
[/code]
<div style="color:Black; background-color:White
<pre> [Serializable]
[XmlType(<span style="color:#A31515 "Transaction")]
<span style="color:Blue public <span style="color:Blue class MyTransactionClass
{
<span style="color:Gray /// <span style="color:Gray <summary>
<span style="color:Gray ///<span style="color:Green Property #1
<span style="color:Gray /// <span style="color:Gray </summary>
[XmlElement(ElementName = <span style="color:#A31515 "PropertyT1")]
<span style="color:Blue public <span style="color:Blue string P1;
.....
}
[/code]
but when serialized, this generates the following xml:
<div style="color:Black; background-color:White
<pre><span style="color:Blue <<span style="color:#A31515 Deal<span style="color:Blue >
......
<span style="color:Blue <<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyT1<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 PropertyT1<span style="color:Blue >
.....
<span style="color:Blue </<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Deal<span style="color:Blue >
[/code]
In other words, the expected "Transaction" element (expected by me, in any case!!) gets dropped and all transactions get munged together. (It appears that the MyTransactionClass embellishment doesnt get written - Ive tried it with the XmlType keyword,
as in the snippet, and also with XmlRoot.)
(Ive also tried using a List<MyTransactionClass>)
Now, as regards my options, I really dont want to implement custom serialization. It is obviously far less effort to use these embellishments if I can. But having said this, my knowledge of these embellishments is being stretched here so I thought Id throw
it open.
So the question is, how can I construct my classes such that, when serialized, I get the structure
<div style="color:Black; background-color:White
<pre><span style="color:Blue <<span style="color:#A31515 Deal<span style="color:Blue >
....
<span style="color:Blue <<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Transaction<span style="color:Blue >
....
<span style="color:Blue </<span style="color:#A31515 Transaction<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Transaction<span style="color:Blue >
....
<span style="color:Blue </<span style="color:#A31515 Transaction<span style="color:Blue >
....
<span style="color:Blue </<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Deal<span style="color:Blue >
[/code]
Thanks in advance,
Pete
View the full article
I have an xml message coming in from a 3rd party system. A typical message is of the form:
<div style="color:Black; background-color:White
<pre><span style="color:Blue <<span style="color:#A31515 Deal<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyD1<span style="color:Blue >......<span style="color:Blue </<span style="color:#A31515 PropertyD1<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyD2<span style="color:Blue >......<span style="color:Blue </<span style="color:#A31515 PropertyD2<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 etc<span style="color:Blue >......<span style="color:Blue </<span style="color:#A31515 etc<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Transaction<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyT1<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 PropertyT1<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyT2<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 PropertyT2<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 etc<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 etc<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Transaction<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Transaction<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyT1<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 PropertyT1<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyT2<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 PropertyT2<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 etc<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 etc<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Transaction<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Deal<span style="color:Blue >
[/code]
so basically we have a primary object (a deal) with a bunch of its own properties, and also with a collection of one or more secondary objects (transactions).
When this message comes into my system, the first thing I want to do with this message is to deserialize it and start passing an object around the place. So Im trying to reverse engineer a couple of classes, a Deal class and a Transaction class, which this
document will deserialize into.
Each class contains the necessary embellishments on their properties (pretty much) to generate the xml above upon serialization, but where Im having trouble is the link between the deal class and the transaction class, so that when a deal object is
serialized, I get the
<div style="color:Black; background-color:White
<pre><span style="color:Blue <<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Transaction<span style="color:Blue >
....
<span style="color:Blue </<span style="color:#A31515 Transaction<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Transactions<span style="color:Blue >
[/code]
structure coming into play.
I have tried having the deal class just having a collection of transactions (which is probably the easiest way to implement this if youre not worried about how the serialized data actually looks). In other words...
<div style="color:Black; background-color:White
<pre> <span style="color:Gray /// <span style="color:Gray <summary>
<span style="color:Gray ///<span style="color:Green The Transaction collection
<span style="color:Gray /// <span style="color:Gray </summary>
[XmlElement(ElementName = <span style="color:#A31515 "Transactions")]
<span style="color:Blue public Collection<MyTransactionClass> Transactions;
[/code]
<div style="color:Black; background-color:White
<pre> [Serializable]
[XmlType(<span style="color:#A31515 "Transaction")]
<span style="color:Blue public <span style="color:Blue class MyTransactionClass
{
<span style="color:Gray /// <span style="color:Gray <summary>
<span style="color:Gray ///<span style="color:Green Property #1
<span style="color:Gray /// <span style="color:Gray </summary>
[XmlElement(ElementName = <span style="color:#A31515 "PropertyT1")]
<span style="color:Blue public <span style="color:Blue string P1;
.....
}
[/code]
but when serialized, this generates the following xml:
<div style="color:Black; background-color:White
<pre><span style="color:Blue <<span style="color:#A31515 Deal<span style="color:Blue >
......
<span style="color:Blue <<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PropertyT1<span style="color:Blue >....<span style="color:Blue </<span style="color:#A31515 PropertyT1<span style="color:Blue >
.....
<span style="color:Blue </<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Deal<span style="color:Blue >
[/code]
In other words, the expected "Transaction" element (expected by me, in any case!!) gets dropped and all transactions get munged together. (It appears that the MyTransactionClass embellishment doesnt get written - Ive tried it with the XmlType keyword,
as in the snippet, and also with XmlRoot.)
(Ive also tried using a List<MyTransactionClass>)
Now, as regards my options, I really dont want to implement custom serialization. It is obviously far less effort to use these embellishments if I can. But having said this, my knowledge of these embellishments is being stretched here so I thought Id throw
it open.
So the question is, how can I construct my classes such that, when serialized, I get the structure
<div style="color:Black; background-color:White
<pre><span style="color:Blue <<span style="color:#A31515 Deal<span style="color:Blue >
....
<span style="color:Blue <<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Transaction<span style="color:Blue >
....
<span style="color:Blue </<span style="color:#A31515 Transaction<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Transaction<span style="color:Blue >
....
<span style="color:Blue </<span style="color:#A31515 Transaction<span style="color:Blue >
....
<span style="color:Blue </<span style="color:#A31515 Transactions<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Deal<span style="color:Blue >
[/code]
Thanks in advance,
Pete
View the full article