473,508 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MC++ & C# generate different XML

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.Serialization;
using System.Collections;

[Serializable]
public class UnitsCollection : ArrayList
{
public Units Add(Units obj) { base.Add(obj); return obj; }
public Units Add() { return Add(new Units()); }
}

[XmlType(TypeName="Units"),XmlRoot,Serializable]
public class Units
{
public Units() { }
[XmlAttribute(AttributeName="name")]
public string __name;
}

[XmlRoot(ElementName="LandXML",IsNullable=false),Se rializable]
public class LandXML
{
public LandXML() { }
[XmlElement(Type=typeof(Units),ElementName="Units", IsNullable=false)]
public UnitsCollection __UnitsCollection;
}

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.__UnitsCollection = units;

XmlSerializer ser = new XmlSerializer(typeof(LandXML));
XmlTextWriter writer = new XmlTextWriter("c_sharp.xml",
System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
ser.Serialize(writer, 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.dll>
#using <System.dll>

using namespace System;
using namespace System::Xml::Serialization;
using namespace System::Xml;
using namespace System::Data;
using namespace System::Reflection;
using namespace System::Collections;

[XmlRootAttribute(ElementName="Units", IsNullable=false),
SerializableAttribute]
public __gc class Units {
public:
Units() { }
[XmlAttributeAttribute(AttributeName="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
*/
[SerializableAttribute, DefaultMemberAttribute("Index")]
public __gc class UnitsCollection : public ArrayList
{
public:
UnitsCollection() { }

Units* Add(Units* obj) { __super::Add(obj); 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<Units*>(__super::Item[index]); }
__property void set_Index(int index, Units* u) { __super::Item[index] =
u; }
};

[XmlRootAttribute(ElementName="LandXML", IsNullable=false),
SerializableAttribute]
public __gc class LandXML {
public:
LandXML() { }

[XmlElement(Type=__typeof(UnitsCollection),ElementN ame="Units",IsNullable=false)]
UnitsCollection* __UnitsCollection;
};

void WriteExceptionInfo(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->__UnitsCollection = units;

XmlSerializer* ser = new XmlSerializer(__typeof(LandXML));
XmlTextWriter* writer = new XmlTextWriter("mc++.xml",
System::Text::Encoding::UTF8);
writer->Formatting = Formatting::Indented;
ser->Serialize(writer, lx);
writer->Close();
}
catch( System::Exception* ex ) {
WriteExceptionInfo(ex);
if( ex!=0 )
WriteExceptionInfo(ex->InnerException);
}

return 0;
}

void WriteExceptionInfo(System::Exception* 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( "StrackTrace: {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
Nov 12 '05 #1
4 3623
"Daniel Lidström" <so*****@microsoft.com> wrote in message news:8g*****************************@40tude.net...
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. : : [XmlType(TypeName="Units"),XmlRoot,Serializable]
public class Units : : [XmlRootAttribute(ElementName="Units", IsNullable=false),
SerializableAttribute]
public __gc class Units { : : Am I missing something, or is the XmlSerializer not working
correct with MC++?


The C# Units class has an XmlTypeAttribute, anonymous XmlRootAttribute,
and SerializableAttribute on it.

The MC++ Units class has an XmlRootAttribute telling it to emit a Units
element, and a SerializableAttribute on it.

Is there some reason these attributes aren't the same between C# and
Managed C++? This could be the cause for receiving the different XML
serializations.
Derek Harmon
Nov 12 '05 #2
"Daniel Lidström" <so*****@microsoft.com> wrote in message news:8g*****************************@40tude.net...
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. : : [XmlType(TypeName="Units"),XmlRoot,Serializable]
public class Units : : [XmlRootAttribute(ElementName="Units", IsNullable=false),
SerializableAttribute]
public __gc class Units { : : Am I missing something, or is the XmlSerializer not working
correct with MC++?


The C# Units class has an XmlTypeAttribute, anonymous XmlRootAttribute,
and SerializableAttribute on it.

The MC++ Units class has an XmlRootAttribute telling it to emit a Units
element, and a SerializableAttribute on it.

Is there some reason these attributes aren't the same between C# and
Managed C++? This could be the cause for receiving the different XML
serializations.
Derek Harmon
Nov 12 '05 #3
On Sat, 29 May 2004 19:47:25 -0400, Derek Harmon wrote:
"Daniel Lidström" <so*****@microsoft.com> wrote in message news:8g*****************************@40tude.net...
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.

::
[XmlType(TypeName="Units"),XmlRoot,Serializable]
public class Units

::
[XmlRootAttribute(ElementName="Units", IsNullable=false),
SerializableAttribute]
public __gc class Units {

::
Am I missing something, or is the XmlSerializer not working
correct with MC++?


The C# Units class has an XmlTypeAttribute, anonymous XmlRootAttribute,
and SerializableAttribute on it.

The MC++ Units class has an XmlRootAttribute telling it to emit a Units
element, and a SerializableAttribute on it.

Is there some reason these attributes aren't the same between C# and
Managed C++? This could be the cause for receiving the different XML
serializations.


Whatever attribute I put on the MC++ Units class I still get the unexpected
xml output. Has anyone been able to generate this kind of xml using MC++?

<?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" />
.... arbitrary number of Units ...
</LandXML>

If so, I would be very happy to see the code that produced it.
Thanks!

--
Daniel
Nov 12 '05 #4
On Sat, 29 May 2004 19:47:25 -0400, Derek Harmon wrote:
"Daniel Lidström" <so*****@microsoft.com> wrote in message news:8g*****************************@40tude.net...
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.

::
[XmlType(TypeName="Units"),XmlRoot,Serializable]
public class Units

::
[XmlRootAttribute(ElementName="Units", IsNullable=false),
SerializableAttribute]
public __gc class Units {

::
Am I missing something, or is the XmlSerializer not working
correct with MC++?


The C# Units class has an XmlTypeAttribute, anonymous XmlRootAttribute,
and SerializableAttribute on it.

The MC++ Units class has an XmlRootAttribute telling it to emit a Units
element, and a SerializableAttribute on it.

Is there some reason these attributes aren't the same between C# and
Managed C++? This could be the cause for receiving the different XML
serializations.


Whatever attribute I put on the MC++ Units class I still get the unexpected
xml output. Has anyone been able to generate this kind of xml using MC++?

<?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" />
.... arbitrary number of Units ...
</LandXML>

If so, I would be very happy to see the code that produced it.
Thanks!

--
Daniel
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3063
by: David | last post by:
Would I be right in saying the following files are equilavent, except first on is HTML and second one is XML? Code: <? echo "<h1>Hello world!</h1>".<br>; echo "This is HTML"; ?>
72
5301
by: Mel | last post by:
Are we going backwards ? (please excuse my spelling...) In my opinion an absolute YES ! Take a look at what we are doing ! we create TAGS, things like <H1> etc. and although there are tools...
0
410
by: Daniel Lidström | last post by:
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...
2
2497
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
3
1331
by: | last post by:
is there any performance diffrence between MC++ 2003 ,other .NET languages and MC++ 2005 (cause of an optimization) ???
1
1243
by: Shawn B. | last post by:
Greetings, With a Managed class, if I'm #including a Windows SDK header file, and call an API, it appears (according to .NET Reflector) that it automatically generates a for each API call that...
0
2235
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
12
10062
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
0
5519
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.