Hi,
I've been trying a long time now to generate some XML using MC++ and
XmlSerializer. I have a piece of C# code that produces exactly what I want,
but I simply can't get the MC++ code to write the same thing. Below I have
included two minimal compilable samples that illustrate my problem.
The C# code produces this XML:
<?xml version="1.0" encoding="utf-8"?>
<LandXML xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance">
<Units name="unit1" />
<Units name="unit2" />
</LandXML>
and the MC++ code produces this:
<?xml version="1.0" encoding="utf-8"?>
<LandXML xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance">
<Units>
<Units name="unit1" />
<Units name="unit2" />
</Units>
</LandXML>
Now follows the C# code:
using System;
using System.Xml;
using System.Xml.Seri alization;
using System.Collecti ons;
[Serializable]
public class UnitsCollection : ArrayList
{
public Units Add(Units obj) { base.Add(obj); return obj; }
public Units Add() { return Add(new Units()); }
}
[XmlType(TypeNam e="Units"),XmlR oot,Serializabl e]
public class Units
{
public Units() { }
[XmlAttribute(At tributeName="na me")]
public string __name;
}
[XmlRoot(Element Name="LandXML", IsNullable=fals e),Serializable]
public class LandXML
{
public LandXML() { }
[XmlElement(Type =typeof(Units), ElementName="Un its",IsNullable =false)]
public UnitsCollection __UnitsCollecti on;
}
public class HelloWorld
{
public static void Main()
{
LandXML lx = new LandXML();
UnitsCollection units = new UnitsCollection ();
Units unit1 = new Units();
Units unit2 = new Units();
unit1.__name = "unit1";
unit2.__name = "unit2";
units.Add(unit1 );
units.Add(unit2 );
lx.__UnitsColle ction = units;
XmlSerializer ser = new XmlSerializer(t ypeof(LandXML)) ;
XmlTextWriter writer = new XmlTextWriter(" c_sharp.xml",
System.Text.Enc oding.UTF8);
writer.Formatti ng = Formatting.Inde nted;
ser.Serialize(w riter, lx);
writer.Close();
}
}
Here is the exact same program written with MC++ (I catch exceptions too):
#using <mscorlib.dll >
#using <System.Xml.dll >
#using <System.Data.dl l>
#using <System.dll>
using namespace System;
using namespace System::Xml::Se rialization;
using namespace System::Xml;
using namespace System::Data;
using namespace System::Reflect ion;
using namespace System::Collect ions;
[XmlRootAttribut e(ElementName=" Units", IsNullable=fals e),
SerializableAtt ribute]
public __gc class Units {
public:
Units() { }
[XmlAttributeAtt ribute(Attribut eName="name")]
System::String* __name;
};
/*
* If I switch Index for Item which I think is what should be used
* I can't compile because of error C2392:
* covariant returns types are not supported in managed types
*/
[SerializableAtt ribute, DefaultMemberAt tribute("Index" )]
public __gc class UnitsCollection : public ArrayList
{
public:
UnitsCollection () { }
Units* Add(Units* obj) { __super::Add(ob j); return obj; }
Units* Add() { return Add(new Units()); }
void Insert(int index, Units* obj) { __super::Insert (index, obj); }
void Remove(Units* obj) { __super::Remove (obj); }
__property Units* get_Index(int index) { return
static_cast<Uni ts*>(__super::I tem[index]); }
__property void set_Index(int index, Units* u) { __super::Item[index] =
u; }
};
[XmlRootAttribut e(ElementName=" LandXML", IsNullable=fals e),
SerializableAtt ribute]
public __gc class LandXML {
public:
LandXML() { }
[XmlElement(Type =__typeof(Units Collection),Ele mentName="Units ",IsNullable=fa lse)]
UnitsCollection * __UnitsCollecti on;
};
void WriteExceptionI nfo(Exception*) ;
int main()
{
try {
LandXML* lx = new LandXML();
UnitsCollection * units = new UnitsCollection ;
Units* unit1 = new Units();
Units* unit2 = new Units();
unit1->__name = "unit1";
unit2->__name = "unit2";
units->Add(unit1);
units->Add(unit2);
lx->__UnitsCollect ion = units;
XmlSerializer* ser = new XmlSerializer(_ _typeof(LandXML ));
XmlTextWriter* writer = new XmlTextWriter(" mc++.xml",
System::Text::E ncoding::UTF8);
writer->Formatting = Formatting::Ind ented;
ser->Serialize(writ er, lx);
writer->Close();
}
catch( System::Excepti on* ex ) {
WriteExceptionI nfo(ex);
if( ex!=0 )
WriteExceptionI nfo(ex->InnerException );
}
return 0;
}
void WriteExceptionI nfo(System::Exc eption* ex)
{
System::Console ::WriteLine( "--------- Exception Data ---------" );
System::Console ::WriteLine( "Message: {0}", ex->Message );
System::Console ::WriteLine( "Exception Type: {0}",
ex->GetType()->FullName );
System::Console ::WriteLine( "Source: {0}", ex->Source );
System::Console ::WriteLine( "StrackTrac e: {0}", ex->StackTrace );
System::Console ::WriteLine( "TargetSite : {0}", ex->TargetSite );
}
Can anyone explain to me what is going on here? Am I missing something, or
is the XmlSerializer not working correct with MC++?
Thank you.
--
Daniel
I've been trying a long time now to generate some XML using MC++ and
XmlSerializer. I have a piece of C# code that produces exactly what I want,
but I simply can't get the MC++ code to write the same thing. Below I have
included two minimal compilable samples that illustrate my problem.
The C# code produces this XML:
<?xml version="1.0" encoding="utf-8"?>
<LandXML xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance">
<Units name="unit1" />
<Units name="unit2" />
</LandXML>
and the MC++ code produces this:
<?xml version="1.0" encoding="utf-8"?>
<LandXML xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance">
<Units>
<Units name="unit1" />
<Units name="unit2" />
</Units>
</LandXML>
Now follows the C# code:
using System;
using System.Xml;
using System.Xml.Seri alization;
using System.Collecti ons;
[Serializable]
public class UnitsCollection : ArrayList
{
public Units Add(Units obj) { base.Add(obj); return obj; }
public Units Add() { return Add(new Units()); }
}
[XmlType(TypeNam e="Units"),XmlR oot,Serializabl e]
public class Units
{
public Units() { }
[XmlAttribute(At tributeName="na me")]
public string __name;
}
[XmlRoot(Element Name="LandXML", IsNullable=fals e),Serializable]
public class LandXML
{
public LandXML() { }
[XmlElement(Type =typeof(Units), ElementName="Un its",IsNullable =false)]
public UnitsCollection __UnitsCollecti on;
}
public class HelloWorld
{
public static void Main()
{
LandXML lx = new LandXML();
UnitsCollection units = new UnitsCollection ();
Units unit1 = new Units();
Units unit2 = new Units();
unit1.__name = "unit1";
unit2.__name = "unit2";
units.Add(unit1 );
units.Add(unit2 );
lx.__UnitsColle ction = units;
XmlSerializer ser = new XmlSerializer(t ypeof(LandXML)) ;
XmlTextWriter writer = new XmlTextWriter(" c_sharp.xml",
System.Text.Enc oding.UTF8);
writer.Formatti ng = Formatting.Inde nted;
ser.Serialize(w riter, lx);
writer.Close();
}
}
Here is the exact same program written with MC++ (I catch exceptions too):
#using <mscorlib.dll >
#using <System.Xml.dll >
#using <System.Data.dl l>
#using <System.dll>
using namespace System;
using namespace System::Xml::Se rialization;
using namespace System::Xml;
using namespace System::Data;
using namespace System::Reflect ion;
using namespace System::Collect ions;
[XmlRootAttribut e(ElementName=" Units", IsNullable=fals e),
SerializableAtt ribute]
public __gc class Units {
public:
Units() { }
[XmlAttributeAtt ribute(Attribut eName="name")]
System::String* __name;
};
/*
* If I switch Index for Item which I think is what should be used
* I can't compile because of error C2392:
* covariant returns types are not supported in managed types
*/
[SerializableAtt ribute, DefaultMemberAt tribute("Index" )]
public __gc class UnitsCollection : public ArrayList
{
public:
UnitsCollection () { }
Units* Add(Units* obj) { __super::Add(ob j); return obj; }
Units* Add() { return Add(new Units()); }
void Insert(int index, Units* obj) { __super::Insert (index, obj); }
void Remove(Units* obj) { __super::Remove (obj); }
__property Units* get_Index(int index) { return
static_cast<Uni ts*>(__super::I tem[index]); }
__property void set_Index(int index, Units* u) { __super::Item[index] =
u; }
};
[XmlRootAttribut e(ElementName=" LandXML", IsNullable=fals e),
SerializableAtt ribute]
public __gc class LandXML {
public:
LandXML() { }
[XmlElement(Type =__typeof(Units Collection),Ele mentName="Units ",IsNullable=fa lse)]
UnitsCollection * __UnitsCollecti on;
};
void WriteExceptionI nfo(Exception*) ;
int main()
{
try {
LandXML* lx = new LandXML();
UnitsCollection * units = new UnitsCollection ;
Units* unit1 = new Units();
Units* unit2 = new Units();
unit1->__name = "unit1";
unit2->__name = "unit2";
units->Add(unit1);
units->Add(unit2);
lx->__UnitsCollect ion = units;
XmlSerializer* ser = new XmlSerializer(_ _typeof(LandXML ));
XmlTextWriter* writer = new XmlTextWriter(" mc++.xml",
System::Text::E ncoding::UTF8);
writer->Formatting = Formatting::Ind ented;
ser->Serialize(writ er, lx);
writer->Close();
}
catch( System::Excepti on* ex ) {
WriteExceptionI nfo(ex);
if( ex!=0 )
WriteExceptionI nfo(ex->InnerException );
}
return 0;
}
void WriteExceptionI nfo(System::Exc eption* ex)
{
System::Console ::WriteLine( "--------- Exception Data ---------" );
System::Console ::WriteLine( "Message: {0}", ex->Message );
System::Console ::WriteLine( "Exception Type: {0}",
ex->GetType()->FullName );
System::Console ::WriteLine( "Source: {0}", ex->Source );
System::Console ::WriteLine( "StrackTrac e: {0}", ex->StackTrace );
System::Console ::WriteLine( "TargetSite : {0}", ex->TargetSite );
}
Can anyone explain to me what is going on here? Am I missing something, or
is the XmlSerializer not working correct with MC++?
Thank you.
--
Daniel