Connecting Tech Pros Worldwide Forums | Help | Site Map

Cleanest way to change order of serialized el's?

Daniel Lidström
Guest
 
Posts: n/a
#1: Nov 12 '05
Hi,

I would like to know the cleanest way to change the serialization of my
Line class from:

<Line staStart="2327.02" length="10.00000003390744">
<End>549016.570965 57945.741122</End>
<Start>549019.590988 57955.274194</Start>
</Line>

to

<Line staStart="2327.02" length="10.00000003390744">
<Start>549019.590988 57955.274194</Start>
<End>549016.570965 57945.741122</End>
</Line>

i.e. the only change is the order of the Start and End elements. This is
really a minor issue but if it does not involve too much work, I'd like to
have it this way. My Line class has this interface:

public __gc class Line {

public:
Line();

[System::Xml::Serialization::XmlAttributeAttribute]
__property double get_length();
__property void set_length(double l);

[System::Xml::Serialization::XmlIgnoreAttribute]
bool dirSpecified;
[System::Xml::Serialization::XmlAttributeAttribute]
__property double get_dir();
__property void set_dir(double d);

[System::Xml::Serialization::XmlIgnoreAttribute]
bool staStartSpecified;
[System::Xml::Serialization::XmlAttributeAttribute]
__property double get_staStart();
__property void set_staStart(double s);

[System::Xml::Serialization::XmlElement]
__property Point3D* get_Start();
__property void set_Start(Point3D* p);

[System::Xml::Serialization::XmlElement]
__property Point3D* get_End();
__property void set_End(Point3D* p);

private:

Point3D *m_start, *m_end;
double m_staStart, m_dir;
};

As you see I'm using MC++. That shouldn't matter though, if you have a
solution in C# I'd gladly accept it. I'm thinking I should put m_start and
m_end in an ArrayList, but to get the desired result it would mean m_start
and m_end would have to be different datatypes (new classes Start and End):

[XmlElementAttribute(Type=__typeof(Start),ElementNa me="Start")]
[XmlElementAttribute(Type=__typeof(End),ElementName ="End")]
__property ArrayList* get_Collection();
__property void set_Collection(ArrayList* arr);

Any other ideas?
Thanks in advance!

--
Daniel
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nigel Armstrong
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Cleanest way to change order of serialized el's?


Hi Daniel

Why don't you try creating a schema file with the xml structure that you
want, then use the xsd tool to generate the class for you? You won't be able
to do this with C++ (No CodeDOM support), but you would get a C# class out of
it...

I tried this (note the simplification of your Start and End elements to
strings - you would probably want to do this as a list of a simple type with
the precision that you require, then limit the list length). And I just used
decimal for the attributes...

So here's the xsd:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Line">
<xs:complexType>
<xs:sequence>
<xs:element name="Start" type="xs:string"/>
<xs:element name="End" type="xs:string"/>
</xs:sequence>
<xs:attribute name="staStart" type="xs:decimal" use="required"/>
<xs:attribute name="length" type="xs:decimal" use="required"/>
</xs:complexType>
</xs:element>


</xs:schema>

With this, the cs class generated contains just public fields - I've
modified the Start and End to use properties, and have this class:

//------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: 1.1.4322.2032
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated
//------------------------------------------------------------------------------

//
// This source code was auto-generated by xsd, Version=1.1.4322.2032.
//
using System.Xml.Serialization;


/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute(Namespac e="", IsNullable=false)]
public class Line {

/// <remarks/>
private string mStart
[System.Xml.Serialization.XmlElementAttribute(Form= System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Start
{
get
{
return mStart;
}
set
{
mStart = value;
}
}

/// <remarks/>
private string mEnd
[System.Xml.Serialization.XmlElementAttribute(Form= System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string End
{
get
{
return mEnd;
}
set
{
mEnd = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.Decimal staStart;

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.Decimal length;
}

When you serialize a Line this with this code:

using System;

namespace TestLine
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//

Line l = new Line();
l.Start = "549019.590988 57955.274194";
l.End = "549016.570965 57945.741122";
l.length = 10.00000003390744M;
l.staStart = 2327.02M;

System.Xml.Serialization.XmlSerializer s = new
System.Xml.Serialization.XmlSerializer(typeof(Line ), "");
s.Serialize(Console.Out, l, null);

}
}
}


then you get this output:

<?xml version="1.0" encoding="UTF-8"?>
<Line xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/
2001/XMLSchema-instance" staStart="2327.02" length="10.00000003390744">
<Start>549019.590988 57955.274194</Start>
<End>549016.570965 57945.741122</End>
</Line>

I'm not sure that this helps you much - in the C# code, when you switch the
order of the Start and End properties, then the serialization also switches...

Nigel Armstrong

"Daniel Lidström" wrote:
[color=blue]
> Hi,
>
> I would like to know the cleanest way to change the serialization of my
> Line class from:
>
> <Line staStart="2327.02" length="10.00000003390744">
> <End>549016.570965 57945.741122</End>
> <Start>549019.590988 57955.274194</Start>
> </Line>
>
> to
>
> <Line staStart="2327.02" length="10.00000003390744">
> <Start>549019.590988 57955.274194</Start>
> <End>549016.570965 57945.741122</End>
> </Line>
>
> i.e. the only change is the order of the Start and End elements. This is
> really a minor issue but if it does not involve too much work, I'd like to
> have it this way. My Line class has this interface:
>
> public __gc class Line {
>
> public:
> Line();
>
> [System::Xml::Serialization::XmlAttributeAttribute]
> __property double get_length();
> __property void set_length(double l);
>
> [System::Xml::Serialization::XmlIgnoreAttribute]
> bool dirSpecified;
> [System::Xml::Serialization::XmlAttributeAttribute]
> __property double get_dir();
> __property void set_dir(double d);
>
> [System::Xml::Serialization::XmlIgnoreAttribute]
> bool staStartSpecified;
> [System::Xml::Serialization::XmlAttributeAttribute]
> __property double get_staStart();
> __property void set_staStart(double s);
>
> [System::Xml::Serialization::XmlElement]
> __property Point3D* get_Start();
> __property void set_Start(Point3D* p);
>
> [System::Xml::Serialization::XmlElement]
> __property Point3D* get_End();
> __property void set_End(Point3D* p);
>
> private:
>
> Point3D *m_start, *m_end;
> double m_staStart, m_dir;
> };
>
> As you see I'm using MC++. That shouldn't matter though, if you have a
> solution in C# I'd gladly accept it. I'm thinking I should put m_start and
> m_end in an ArrayList, but to get the desired result it would mean m_start
> and m_end would have to be different datatypes (new classes Start and End):
>
> [XmlElementAttribute(Type=__typeof(Start),ElementNa me="Start")]
> [XmlElementAttribute(Type=__typeof(End),ElementName ="End")]
> __property ArrayList* get_Collection();
> __property void set_Collection(ArrayList* arr);
>
> Any other ideas?
> Thanks in advance!
>
> --
> Daniel
> A: Because it messes up the order in which people normally read text.
> Q: Why is top-posting such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?
>[/color]
Daniel Lidström
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Cleanest way to change order of serialized el's?


Hi Higel,


On Wed, 24 Nov 2004 11:53:03 -0800, Nigel Armstrong wrote:
[color=blue]
> Hi Daniel
>
> Why don't you try creating a schema file with the xml structure that you
> want, then use the xsd tool to generate the class for you? You won't be able
> to do this with C++ (No CodeDOM support), but you would get a C# class out of
> it...
>
> I tried this (note the simplification of your Start and End elements to
> strings - you would probably want to do this as a list of a simple type with
> the precision that you require, then limit the list length). And I just used
> decimal for the attributes...[/color]

Thanks for your comments,
I have made a smallest possible program that shows my problem:

#using <mscorlib.dll>
#using <System.Xml.dll>

using namespace System::Xml::Serialization;
using namespace System;

public __gc class Line {

public:
Line() {
m_start = "1 2";
m_end = "3 4";
}


[XmlElementAttributeAttribute(Form=System::Xml::Sch ema::XmlSchemaForm::Unqualified)]
__property String* get_Start() { return m_start; }
__property void set_Start(String* s) { m_start = s; }


[XmlElementAttributeAttribute(Form=System::Xml::Sch ema::XmlSchemaForm::Unqualified)]
__property String* get_End() { return m_end; }
__property void set_End(String* e) { m_end = e; }

private:

String* m_start;
String* m_end;
};

int main()
{
Line* l = new Line();
l->Start = "start";
l->End = "end";

XmlSerializer* s = new XmlSerializer(__typeof(Line));
s->Serialize(Console::Out, l, 0);

return 0;
}

This code produces the following output:

<?xml version="1.0" encoding="ibm850"?>
<Line xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<End>end</End>
<Start>start</Start>
</Line>

As you see, the End element is still ahead of the Start element. How can it
be that the equivalent program in C# gets this right (see Nigel's program)?
Would somebody who knows a bit more about this care to explain (are you
reading this Dino...)? Many thanks in advance!

--
Daniel
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Closed Thread