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:
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?