![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Question Re XML Serialization Order I'm having problem with the XML Serialization in how it orders the generated XML with derived classes. This code is slightly modified from one of the examples available on MSDN, in that it runs in a windows form. This example returns a file with this XML: <?xml version="1.0" encoding="utf-8" ?> - <Orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> - <NewBook> <ISBN>123456789</ISBN> <NewEdition>true</NewEdition> </NewBook> </Orders> The <ISBN> is coming from the base class and <NewEdition> is coming from the derived class. The problem I have is that I need to be able to reverse these as in: <?xml version="1.0" encoding="utf-8" ?> - <Orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> - <NewBook> <NewEdition>true</NewEdition> <ISBN>123456789</ISBN> </NewBook> </Orders> The code as modified is: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml.Serialization; using System.Runtime.InteropServices; using System.Reflection; using System.Net; using System.Collections; using System.IO; namespace TestSerialization { public class Book { public string ISBN; } public class ExpandedBook : Book { public bool NewEdition; } public class Orders { public Book[] Books; } public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } public void SerializeObject(string filename) { // Each overridden field, property, or type requires // an XmlAttributes instance. XmlAttributes attrs = new XmlAttributes(); // Creates an XmlElementAttribute instance to override the // field that returns Book objects. The overridden field // returns Expanded objects instead. XmlElementAttribute attr = new XmlElementAttribute(); attr.ElementName = "NewBook"; attr.Type = typeof(ExpandedBook); // Adds the element to the collection of elements. attrs.XmlElements.Add(attr); // Creates the XmlAttributeOverrides instance. XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); // Adds the type of the class that contains the overridden // member, as well as the XmlAttributes instance to override it // with, to the XmlAttributeOverrides. attrOverrides.Add(typeof(Orders), "Books", attrs); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(typeof(Orders), attrOverrides); // Writing the file requires a TextWriter instance. TextWriter writer = new StreamWriter(filename); // Creates the object to be serialized. Orders myOrders = new Orders(); // Creates an object of the derived type. ExpandedBook b = new ExpandedBook(); b.NewEdition = true; b.ISBN = "123456789"; myOrders.Books = new ExpandedBook[] { b }; // Serializes the object. s.Serialize(writer, myOrders); writer.Close(); } private void button1_Click(object sender, EventArgs e) { SerializeObject(textBox1.Text); textBox1.Text = "Done"; } } } Any help that anyone can give would be appreciated. Dave |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Question Re XML Serialization Order I've attached a useful project which demonstrates what we are trying to accomplish. Run this project and press Serialize. This will gnenerate the following into the text output area: <?xml version="1.0" encoding="utf-16"?> <Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <ISBN>987654321</ISBN> </Book> <?xml version="1.0" encoding="utf-16"?> <ExpandedBook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <ISBN>123456789</ISBN> <NewEdition>true</NewEdition> </ExpandedBook> The first block is the serialization of the Book (base) class. The second block is the serialization of the ExpandedBook (derived) class. Note that the serializer gives precedence to ISBN from the base class, before emitting NewEdition from the derived class. For whatever reason, we'd like to reverse this, so that we'll get the elements in the reverse order: <NewEdition>true</NewEdition> <ISBN>123456789</ISBN> This is a trivial example of a much more complex production issue. However, if we can get this simple issue squared away, we'll be able to apply the principle to our complex case to arrive at the solution we need. Thanks very much for any assistance which you can provide! Joseph Geretz --------------------------- "David Wheeler" <dave@xxxxxx> wrote in message news:uWGlqR1TJHA.4452@xxxxxx I'm having problem with the XML Serialization in how it orders the generated XML with derived classes. This code is slightly modified from one of the examples available on MSDN, in that it runs in a windows form. This example returns a file with this XML: <?xml version="1.0" encoding="utf-8" ?> - <Orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> - <NewBook> <ISBN>123456789</ISBN> <NewEdition>true</NewEdition> </NewBook> </Orders> The <ISBN> is coming from the base class and <NewEdition> is coming from the derived class. The problem I have is that I need to be able to reverse these as in: <?xml version="1.0" encoding="utf-8" ?> - <Orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> - <NewBook> <NewEdition>true</NewEdition> <ISBN>123456789</ISBN> </NewBook> </Orders> The code as modified is: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml.Serialization; using System.Runtime.InteropServices; using System.Reflection; using System.Net; using System.Collections; using System.IO; namespace TestSerialization { public class Book { public string ISBN; } public class ExpandedBook : Book { public bool NewEdition; } public class Orders { public Book[] Books; } public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } public void SerializeObject(string filename) { // Each overridden field, property, or type requires // an XmlAttributes instance. XmlAttributes attrs = new XmlAttributes(); // Creates an XmlElementAttribute instance to override the // field that returns Book objects. The overridden field // returns Expanded objects instead. XmlElementAttribute attr = new XmlElementAttribute(); attr.ElementName = "NewBook"; attr.Type = typeof(ExpandedBook); // Adds the element to the collection of elements. attrs.XmlElements.Add(attr); // Creates the XmlAttributeOverrides instance. XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); // Adds the type of the class that contains the overridden // member, as well as the XmlAttributes instance to override it // with, to the XmlAttributeOverrides. attrOverrides.Add(typeof(Orders), "Books", attrs); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(typeof(Orders), attrOverrides); // Writing the file requires a TextWriter instance. TextWriter writer = new StreamWriter(filename); // Creates the object to be serialized. Orders myOrders = new Orders(); // Creates an object of the derived type. ExpandedBook b = new ExpandedBook(); b.NewEdition = true; b.ISBN = "123456789"; myOrders.Books = new ExpandedBook[] { b }; // Serializes the object. s.Serialize(writer, myOrders); writer.Close(); } private void button1_Click(object sender, EventArgs e) { SerializeObject(textBox1.Text); textBox1.Text = "Done"; } } } Any help that anyone can give would be appreciated. Dave |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Question Re XML Serialization Order David Wheeler skrev: Quote: > I'm having problem with the XML Serialization in how it orders the > generated XML with derived classes. This code is slightly modified from > one of the examples available on MSDN, in that it runs in a windows > form. This example returns a file with this XML: Quote: > *-* <NewBook> > * * <ISBN>*123456789*</ISBN> > * * <NewEdition>*true*</NewEdition> > * * </NewBook> Quote: > The <ISBN> is coming from the base class and <NewEdition> is coming from > the derived class. The problem I have is that I need to be able to > reverse these as in: Quote: > *-* <NewBook> > <NewEdition>*true*</NewEdition> > * * <ISBN>*123456789*</ISBN> > * * </NewBook> I have only one question, - why and does it matter? -- Bjørn Brox |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Question Re XML Serialization Order Hi Bjørn, It matters because the remote consumer of our XML processes certain elements of the XML in positional order. I can't speak for them positively, but I suspect that their need to process high volumes of these transactions in XML format has let them to positional element access which is more efficient than traditional XPath access? I could be wrong about this, but I am correct about the bottom line which is that we need to suply them the XML with certain elements in specific ordinal position, otherwise our transaction fails. Hence, the reason for this posting. I hope this answers your question. Can you answer ours? Thanks, Joseph Geretz "Bjørn Brox" <bpbrox@xxxxxx> wrote in message news:492d0705@xxxxxx Quote: > David Wheeler skrev: Quote: >> I'm having problem with the XML Serialization in how it orders the >> generated XML with derived classes. This code is slightly modified from >> one of the examples available on MSDN, in that it runs in a windows form. >> This example returns a file with this XML: Quote: >> *-* <NewBook> >> * * <ISBN>*123456789*</ISBN> >> * * <NewEdition>*true*</NewEdition> >> * * </NewBook> Quote: >> The <ISBN> is coming from the base class and <NewEdition> is coming from >> the derived class. The problem I have is that I need to be able to >> reverse these as in: Quote: >> *-* <NewBook> >> <NewEdition>*true*</NewEdition> >> * * <ISBN>*123456789*</ISBN> * * </NewBook> > I have only one question, - why and does it matter? > > -- > Bjørn Brox |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Question Re XML Serialization Order http://sholliday.spaces.live.com/Blo...842A!148.entry Push come to shove, you can do an xml to xml transformation. "Joseph Geretz" <jgeretz@xxxxxx> wrote in message news:eFhB3R9TJHA.5024@xxxxxx Quote: > Hi Bjørn, > > It matters because the remote consumer of our XML processes certain > elements of the XML in positional order. I can't speak for them > positively, but I suspect that their need to process high volumes of these > transactions in XML format has let them to positional element access which > is more efficient than traditional XPath access? > > I could be wrong about this, but I am correct about the bottom line which > is that we need to suply them the XML with certain elements in specific > ordinal position, otherwise our transaction fails. Hence, the reason for > this posting. > > I hope this answers your question. Can you answer ours? > > Thanks, > > Joseph Geretz > > "Bjørn Brox" <bpbrox@xxxxxx> wrote in message > news:492d0705@xxxxxx Quote: >> David Wheeler skrev: Quote: >>> I'm having problem with the XML Serialization in how it orders the >>> generated XML with derived classes. This code is slightly modified from >>> one of the examples available on MSDN, in that it runs in a windows >>> form. This example returns a file with this XML: Quote: >>> *-* <NewBook> >>> * * <ISBN>*123456789*</ISBN> >>> * * <NewEdition>*true*</NewEdition> >>> * * </NewBook> Quote: >>> The <ISBN> is coming from the base class and <NewEdition> is coming from >>> the derived class. The problem I have is that I need to be able to >>> reverse these as in: Quote: >>> *-* <NewBook> >>> <NewEdition>*true*</NewEdition> >>> * * <ISBN>*123456789*</ISBN> * * </NewBook> >> I have only one question, - why and does it matter? >> >> -- >> Bjørn Brox > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Question Re XML Serialization Order Joseph Geretz skrev: Quote: > Hi Bjørn, > > It matters because the remote consumer of our XML processes certain elements > of the XML in positional order. I can't speak for them positively, but I > suspect that their need to process high volumes of these transactions in XML > format has let them to positional element access which is more efficient > than traditional XPath access? > > I could be wrong about this, but I am correct about the bottom line which is > that we need to suply them the XML with certain elements in specific ordinal > position, otherwise our transaction fails. Hence, the reason for this > posting. > > I hope this answers your question. Can you answer ours? > Tried the Order parameter in front of the elements you want to serialize? [XmlElement(Order = 1)] public bool NewEdition; .... [XmlElement(Order = 2)] public string ISBN; -- Bjørn Brox |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Question Re XML Serialization Order Thanks Bjørn, Unfortunately your suggestion does not work because the [XmlElement(Order = n)] directives are scoped within class. Thus, all properties from the derived class are always emitted to the serialization after all properties from the base class. Within these two groups the order can be controlled as you say, however in our case we need serialization from the derived class to precede the serialization from the base class which is opposite of the normal serialization sequence. (You can use the project which I submitted to test this asertion. Decorate the properties with varying ordering sequences. It won't make a difference. You'll still get ISBN (base class) first and NewEdition (derived class) second.) Thanks, Joseph Geretz "Bjørn Brox" <bpbrox@xxxxxx> wrote in message news:492d6f28$1@xxxxxx Quote: > Joseph Geretz skrev: Quote: >> Hi Bjørn, >> >> It matters because the remote consumer of our XML processes certain >> elements of the XML in positional order. I can't speak for them >> positively, but I suspect that their need to process high volumes of >> these transactions in XML format has let them to positional element >> access which is more efficient than traditional XPath access? >> >> I could be wrong about this, but I am correct about the bottom line which >> is that we need to suply them the XML with certain elements in specific >> ordinal position, otherwise our transaction fails. Hence, the reason for >> this posting. >> >> I hope this answers your question. Can you answer ours? >> > Sorry for not including the answer... > > Tried the Order parameter in front of the elements you want to serialize? > > [XmlElement(Order = 1)] > public bool NewEdition; > ... > [XmlElement(Order = 2)] > public string ISBN; > > -- > Bjørn Brox |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Question Re XML Serialization Order Hi Sloan, This is true. Server side XSL transformations are not cheap, in our experience. We're still hoping for a solution which will deliver the XML to us in the format we need without having to go through the secondary transformation step. Thanks for your suggestion. Joseph Geretz "sloan" <sloan@xxxxxx> wrote in message news:OBePu29TJHA.5244@xxxxxx Quote: > http://sholliday.spaces.live.com/Blo...842A!148.entry > > Push come to shove, you can do an xml to xml transformation. > > > "Joseph Geretz" <jgeretz@xxxxxx> wrote in message > news:eFhB3R9TJHA.5024@xxxxxx Quote: >> Hi Bjørn, >> >> It matters because the remote consumer of our XML processes certain >> elements of the XML in positional order. I can't speak for them >> positively, but I suspect that their need to process high volumes of >> these transactions in XML format has let them to positional element >> access which is more efficient than traditional XPath access? >> >> I could be wrong about this, but I am correct about the bottom line which >> is that we need to suply them the XML with certain elements in specific >> ordinal position, otherwise our transaction fails. Hence, the reason for >> this posting. >> >> I hope this answers your question. Can you answer ours? >> >> Thanks, >> >> Joseph Geretz >> >> "Bjørn Brox" <bpbrox@xxxxxx> wrote in message >> news:492d0705@xxxxxx Quote: >>> David Wheeler skrev: >>>> I'm having problem with the XML Serialization in how it orders the >>>> generated XML with derived classes. This code is slightly modified >>>> from one of the examples available on MSDN, in that it runs in a >>>> windows form. This example returns a file with this XML: >>> ... >>>> *-* <NewBook> >>>> * * <ISBN>*123456789*</ISBN> >>>> * * <NewEdition>*true*</NewEdition> >>>> * * </NewBook> >>> .. >>>> The <ISBN> is coming from the base class and <NewEdition> is coming >>>> from the derived class. The problem I have is that I need to be able to >>>> reverse these as in: >>> ... >>>> *-* <NewBook> >>>> <NewEdition>*true*</NewEdition> >>>> * * <ISBN>*123456789*</ISBN> * * </NewBook> >>> ... >>> I have only one question, - why and does it matter? >>> >>> -- >>> Bjørn Brox >> > |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Question Re XML Serialization Order Joseph Geretz skrev: Quote: > Thanks Bjørn, > > Unfortunately your suggestion does not work because the [XmlElement(Order = > n)] directives are scoped within class. Thus, all properties from the > derived class are always emitted to the serialization after all properties > from the base class. Within these two groups the order can be controlled as > you say, however in our case we need serialization from the derived class to > precede the serialization from the base class which is opposite of the > normal serialization sequence. > > (You can use the project which I submitted to test this asertion. Decorate > the properties with varying ordering sequences. It won't make a difference. > You'll still get ISBN (base class) first and NewEdition (derived class) > second.) > I don't bother try to test this, but you can suppress serialization too, and serialize elements with another name than declared. [XmlIgnore] public bool NewEdition; .... [XmlElement(ElementName="NewEdition")] public string ETrick { get { return NewEdition; } set { NewEdition = value } } public string ISBN; Quote: Quote: >> Tried the Order parameter in front of the elements you want to serialize? >> >> [XmlElement(Order = 1)] >> public bool NewEdition; >> ... >> [XmlElement(Order = 2)] >> public string ISBN; >> >> -- >> Bjørn Brox > -- Bjørn Brox |
My System Specs![]() |
| | #10 (permalink) |
| | Re: Question Re XML Serialization Order Hi Bjørn, Thanks for sticking with this. Unfortunately, this won't work for us since sometimes the base transaction class is used by itself for basic transactions, and sometimes the derived class is used for special transactions. We can't suppress XML Elements from the base class because if we do that the base class won't serialize properly. The problem is that the XMLSerializer doesn't properly aggregate the derived and base class elements as a single unit. Well, actually, it does and it doesn't. It does, for the purpose of aggregating all serialized elements into a single parent element. However, it doesn't for the purpose of applying XMLElement Order directives. This is the unfortunate point. We played around with the idea of aggregation / containment vs inheritance. This didn't work for us, since as soon as containment is implemented, the container class is serialized as an XMLElement (which is fine) but then each contained class is serialized as a separate, nested Element, before their respective properties are serialized as third level elements *of their respective and different* parent (second level) Elements. This is just the wrong structure for us. Your XMLIgnore suggestion gave me an idea and I tried to apply it to the Class declaration of each contained class, hoping to suppress the second level Element, however this is illegal. XMLIgnore cannot be applied to the class declaration. Thanks for your help with this, Joseph Geretz "Bjørn Brox" <bpbrox@xxxxxx> wrote in message news:492d93a0$1@xxxxxx Quote: > Joseph Geretz skrev: Quote: >> Thanks Bjørn, >> >> Unfortunately your suggestion does not work because the [XmlElement(Order >> = n)] directives are scoped within class. Thus, all properties from the >> derived class are always emitted to the serialization after all >> properties from the base class. Within these two groups the order can be >> controlled as you say, however in our case we need serialization from the >> derived class to precede the serialization from the base class which is >> opposite of the normal serialization sequence. >> >> (You can use the project which I submitted to test this asertion. >> Decorate the properties with varying ordering sequences. It won't make a >> difference. You'll still get ISBN (base class) first and NewEdition >> (derived class) second.) >> > I don't bother try to test this, but you can suppress serialization too, > and serialize elements with another name than declared. > > [XmlIgnore] > public bool NewEdition; > ... > [XmlElement(ElementName="NewEdition")] > public string ETrick { > get { return NewEdition; } > set { NewEdition = value } > } > > public string ISBN; > > Quote: Quote: >>> Tried the Order parameter in front of the elements you want to >>> serialize? >>> >>> [XmlElement(Order = 1)] >>> public bool NewEdition; >>> ... >>> [XmlElement(Order = 2)] >>> public string ISBN; >>> >>> -- >>> Bjørn Brox >> > > -- > Bjørn Brox |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Bootup order/terminology question | General Discussion | |||
| Order of installation, a general question... | Vista General | |||
| harry potter and the order of the phoenix question | Vista Games | |||
| DNS Search Order Question | Vista networking & sharing | |||