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),Serializable]
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),ElementName="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
0
someone913 (592)
5/28/2004 12:26:50 PM
dotnet.xml 7266 articles. 0 followers. Follow

2 Replies
775 Views

Similar Articles

[PageSpeed] 11

"Daniel Lidstr�m" <someone@microsoft.com> wrote in message news:8glk7c8htkn9.17nh5w3guyfb4$.dlg@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


0
loresayer (274)
5/29/2004 11:47:25 PM
On Sat, 29 May 2004 19:47:25 -0400, Derek Harmon wrote:

> "Daniel Lidstr�m" <someone@microsoft.com> wrote in message news:8glk7c8htkn9.17nh5w3guyfb4$.dlg@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
0
someone913 (592)
6/21/2004 7:33:42 AM
Reply:

Similar Artilces:

difference send as / receive as
Hi, I know that if you want under E2K "service account" access to mailboxes you must give yourself "send as"/ and "receive as" access ". But can anyone please tell me the whaht is the difference between the two permissions ? Regards Jenny Jenny wrote: > Hi, > > I know that if you want under E2K "service account" access to mailboxes you > must give yourself "send as"/ and "receive as" access ". But can anyone > please tell me the whaht is the difference between the two permissions ? > > Regards Je...

Auto Account Number Generation
Here's a newbie question for which I have not yet been able to find a solution. Whenever creating a new account, I'd like the form to automatically display an account number in the accountnumber field that is one more than the max in the table. How can I automtically create this? That is how can I run a query on form load that looks up the current max account number then add one to it? ANy help would be appreciated. Only way to do this is through writing some code. For example you could achieve this using a callout. When you save a new account you can have the callout scan ...

Differences in various auto-fit options (2007)
Does anyone know why these two autofit mechanisms give different results? 1. Right-click in table | AutoFit | AutoFit to Contents. 2. Select entire table | Double-click right border. Right-clicking in a table brings up a list of options. One of them is Auto-Fit. Selecting Auto-Fit, brings up three options: AutoFit to Contents (AFTC) AutoFit to Window (AFTW) Fixed Column Width (FCW) AFTW makes the table as wide as possible. I am not sure how it decides how wide to make each column. AFTC doesn't seem to work all the time. I have a table with 18 columns on a le...

Counting how many different names in a long list
I have a list of 332 names many of them are duplicated. I am looking for a formula or function that can count how many unique names there are in the list. Can anyone help? -- Quaisne ------------------------------------------------------------------------ Quaisne's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=28052 View this thread: http://www.excelforum.com/showthread.php?threadid=501357 This'll count the number of distinct values in a range: =SUMPRODUCT((A1:A99<>"")/COUNTIF(A1:A99,A1:A99&"")) (adjust the range to matc...

Exchange server and huge emails generated to postmaster
I have been trying find a solution for a serious problem on exchanger server 2003 (on SBS2003). Recently probably due to a virus infection a lot emails were being generated to postmaster (1 million in 10 hours) plus badmail in badmail queue. Have checked it with norton corporate a/v but can not find any viruses. Also tried stinger but no viruses found. Have emptied all the queues. Used microsofts anti-adware program to check for adware and trojans. It came up clean. Still getting loads of emails. The only way to stop it is to stop the exchange information store service, but just dismounting...

Serializing Array with array "name" in the XML
I need help serializing an array without including the array "name". I am writing in C# and using the XmlSerializer to serial classes. I am trying to serialize a class with an array in it like the one below. public class myclass { public imagecontext; public myimage[] images; } public myimage { public string name; public string path; } It will serialize something like this... <myclass> <imagecontext>my context</imagecontext> <images> <myimage> <name>Image1</name> ...

xml config file...
hello, I ve a Windowsapplication with a configuration file app.config... (it's in xml) code app.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name = "MyNameValueSection" type="System.Configuration.NameValueSectionHandler" /> </configSections> <MyNameValueSection> <add key="MyKey" value="MyValue" /> </MyNameValueSection> <appSettings> <add key="test" value="MeinInhalt" /> </appSettings> </configurati...

A problem occur after changing OFFICE from C: to D:
Considering my C:\ is nearly full, I unstall my office from C:\ and then install my Office to D:\ After this, OFFICE runs well except EXCEL has a warning messege when I open it. The problem is: When I open EXCEL, it jump up an alert messege: [Unable to find C:\Program Files\Microsoft Office\Office10\Library\Analysis\ATPVBACS.XLA] 1. Can anyone tell me how to solve this problem? 2. If I don't solve this problem, what will be the result? Thanks! Martin 2007-07-04 > The problem is: > > When I open EXCEL, it jump up an alert messege: > [Unable to find C:\Program Fil...

Calculating date difference
Hi I need to calculate the number of years of service for person(difference between Start_Date & End_Date). The result should b in terms of years, months and days (all in one single cell). Ca someone please help? Thanks ----------------------------------------------- ~~ Message posted from http://www.ExcelTip.com ~~View and post usenet messages directly from http://www.ExcelForum.com Have a look at Chip's site: http://www.cpearson.com/excel/datedif.htm Andy. "rll" <rll.xjm6a@excelforum-nospam.com> wrote in message news:rll.xjm6a@excelforum-nospam.com... > &...

Render XML data island to HTML?
I have an XML data island in my aspx web page. The XML includes a link (processing instruction) to an XSL stylesheet. Is there any way on this page to view the HTML, that results from the xsl transformation, as the contents of, say, a div? I know I can do it if I display only the XML in IE, but I don't know if I can do it as a fragment of an existing HTML page. TIA > I have an XML data island in my aspx web page. The XML includes a link > (processing instruction) to an XSL stylesheet. Is there any way on this page > to view the HTML, that results from the xsl transformation, a...

Corporate or Workgroup, send from different accounts
I have been using Outlook 2000 (CW) in order to attach history to my contacts in ACT! This has been running well. The problem I've run into is I used to be able click on the "Send" button and choose a different email account to send from, I no longer have that option since I set up the workgroup settings. The email account that I can't send from is a Linux firewall account we use for internal emails...I can receive emails, but I can't reply or send new messages. They keep coming back as undeliverable. Any suggestions/feedback would be greatly appreciated. ...

Mapisp32.exe has generated errors and will be shut down
One of my coworkers is getting this error whenever Outlook97 checks for email. She is running Office97 Pro in WIndows 2000 on a 600mhz PIII with 128MB RAM. >-----Original Message----- >One of my coworkers is getting this error whenever >Outlook97 checks for email. She is running Office97 Pro in >WIndows 2000 on a 600mhz PIII with 128MB RAM. >. > I almost forgot to mention that I tried the fix listed in Microsoft Article 296412, with no success. Here is the URL to that fix. http://support.microsoft.com/default.aspx?scid=kb;en- us;296412 ...

Efficiently Generate 88 New Chart Sheets
Gang, I've got a simple X*Y chart with year (1977-present) on the x-axis and an estimate of deer population size on the y-axis (actually, there may ultimately be 2 estimates/series). There is nothing really all that fancy about it. It will be updated each year with the current year's estimate. The problem is, I need 87 more just like it - 1 for each of the 88 units that I manage. I have been reading VBA and Excel books and I have yet to find an example on how to "mass produce" a bunch of chart sheets. I take that back - a simple "FOR NEXT" loop will generate t...

Use Stored Procedure developed in C# in a SQL Query
I created a Stored Procedure by selecting a SQL Server Project in Visual Studio 2008 using C#. It receives a SqlString as a parameter and return how many times a specific character was found. I deployed the procedure to a SQL Server database and can see it listed under the database. One caveat is that it has a small lock icon on it. I have a select query where I want to use the stored procedure. I am thinking this is doable in a select query but can't seem to get it to work. What I want to do is to have a select statement listing some fields and one of the fields being th...

XML error "Some Data Was Imported As Text"
Daily, I recieve a 3rd party XML file that I parse in Excel (using an excel macro) and then import into an Access database. Infrequently, the data will import into Excel, then parse and then the routine stops with the error message above. I look through the raw XML file and everything appears to be within the brackets, etc. Can someone give me some clarification to the meaning of the error message? Thanks in advance and Mery Christmas and Happy Holidays! The error message might be generated within your macro - as it is parsing the data it might detect that some field which should be ...

Writing XML from Dataset to Response Object as Binary File
I have looked fot his solution, but Im not sure what to search for. I have an asp.net based report. I need the user to be able to download an XML file of the results so they can use the data in Excel, etc. I do NOT want to write the file to the server first. I just want to be able to write the contents of the Dataset into a binary file and send it back out to the user as a download, but all in memory. Can you give me a boost in the right direction or an article that demonstrates this? Dan Hanover wrote: > I have an asp.net based report. I need the user to be able to download &g...

MFC String-table in C# ?
Hi, maybe a bit OT for this newsgroup, but I know that people here use C#, too. So, does anyone know how the MFC string-table technique map to C#/WinForm? I initially thought about using an integer->string map in C#, but is there some built-in mechanism to manage that? Thanks, Giovanni "Giovanni Dicanio" <giovanniDOTdicanio@REMOVEMEgmail.com> ha scritto nel messaggio news:ugqw1g7LJHA.1736@TK2MSFTNGP03.phx.gbl... > So, does anyone know how the MFC string-table technique map to C#/WinForm? ....after some web search, it seems that C# has a ResourceManager class ...

How do I automatically plot different ranges of data in different.
I need to chart parameters that are a function of three variables in the 2002 version of Excel. I have used the x and y axis to represent 2 variables on an 'xy chart', but require an automatic method to represent the value of the third variable. I have tried the 'bubble-plot', but cannot get sufficient resolution between the data. Is their any way that I may plot the third variable in different colours/shades, that automatically changes for different ranges of the the third variable? THis might help you: http://www.peltiertech.com/Excel/Charts/format.html#CondChart &q...

XML #4
How does one generate a XML code? If you have data in a dataset, you can have the dataset do it automatically: Dim x as string = DataSet.GetXML "Raj" <Raj@discussions.microsoft.com> wrote in message news:BC61AFF2-84EE-425E-8F05-1B02D2A9F317@microsoft.com... > How does one generate a XML code? ...

XML deserialization from string
Hi, I have an XSD and created a VB.NET class file from it. When I have an XML file (conforming to the schema), it is straight forward to deserialize using the class file. I am looking for ways to deserialize when I have the XML as a string and not in a file. Writing the string to a file and deserializing from the file might have an adverse effect on the performance. Anyone know of a good method to do this? Any help will be appreciated. Thanks, Sampath. Use a StringReader to read the file and wrap that in an XmlTextReader. -- This posting is provided "AS IS" with no warranties,...

DataSet from XML
There are several options on my WinForms app where the user can change/add or delete options - these settings are saved in an XML file. When the main form opens, the XML file (and schema) is read into a dataset and the options are displayed in the various controls. Pretty basic... but I am still rather green... My questions is this - when the user makes a change to one of the settings, how to I modify the dataset and display the new data in the controls? Do I create a new dataset in each method? That would seem inefficient as the user could makes dozens of changes - each time creating a n...

Operator Overload between different types
I'm trying to implement the overload for two different kinds of objects. I tryed for example: Dog1 + Dog2; Dog Dog::operator+(Dog& s) { return this->nBau + s.nBau; } it works fine. I need something... Dog1 + Cat1; ??? Dog::operator+(Cat& s) { return ???; } How can i implement my overload between different types and what type of object must return? I can only compile my program only specifing "return 0" in overload function but it doesn't work. Maybe i must implement overload function as static function outside class Dog? Some help please. My final n...

Automating Different Survey Results
Does anyone have any idea on how to facilitate the preparation of surve results? Our survey results are downloaded from our survey online facility (som facility when it can't even produce the report :sigh:). These survey are about the services of the different support groups of our company So each survey has its own set of questions. The backend process o translating these results is quite tedious and I've been thinking o how to automate it ... I know that there is a better way. I am usin Excel but only the standard features such as countif, sumproduct pivot, charts. But still it c...

different colors for x-Value axis
I want to use different colors for X-Values in X-axis of a chart. Is it possible? Example I-1, I-2 black II-1, II-2, II-3 grey III-1, III-2 red IV pink V-1, V-2, V-3 green -- Rasoul Khoshravan Azar Civil Engineer, Osaka, Japan The best solution is that, Excel brings the formatting of x-axis value from source cell. -- Rasoul Khoshravan Azar Civil Engineer, Osaka, Japan "Khoshravan" wrote: > I want to use different colors for X-Values in X-axis of a chart. Is it > possible? > Example > I-1, I-2 black > II-1, II-2, II-3 grey > III-1, III-2 red > IV pink >...

What's the Difference Between This NG and microsoft.public.excel.misc?
See Subject. -- W. eWatson (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39� 15' 7" N, 121� 2' 32" W, 2700 feet Web Page: <www.speckledwithstars.net/> Hi, You are posting to the microsoft.public.excel so I'm not sure what you mean by the subject line? your's ends in .m which I don't think is a legal site. The microsoft Excel newgroup area has many sub-areas dealing with: Functions General Questions New users Charts Excel Application Errors Programming ...