serialize c# class with xsi:type attribute

This may be a simple question, but I'm struggling to find the answer
to it, so any suggestions would be greatly appreciated.... I want to
know if there's some way to ensure that the xsi:type attribute is
included when serializing a c# class.

The basic situation is this:
I have a class, ClassB which derives from ClassA.
ClassC contains a  list of ClassB, which I want to serialize as an
XmlArray of 'bListItem's as per the code below:

    [Serializable]
    [XmlType("classA")]
    public class ClassA
    {
        private string _id;

        [XmlAttribute("id")]
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }
    }

    [Serializable]
    [XmlType("classA.classB")]
    public class ClassB : ClassA
    {
        private string _name;

        [XmlAttribute("name")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }

    [Serializable]
    [XmlType("classC")]
    public class ClassC
    {
        private string _id;
        private List<ClassB> _bList;


        [XmlAttribute("id")]
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [XmlArray("bList"), XmlArrayItem("bListItem")]
        public List<ClassB> BList
        {
            get { return _bList; }
            set { _bList = value; }
        }

    }


Now... this serializes to the following XML:

 <?xml version="1.0" encoding="utf-8" ?>
<classC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="C1">
<bList>
         <bListItem id="B1" name="My B Item" />
    </bList>
 </classC>

What I want to do is have the xsi:type of bListItem come through in
this element like so:

 <?xml version="1.0" encoding="utf-8" ?>
<classC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="C1">
<bList>
         <bListItem xsi:type="classA.classB" id="B1" name="My B Item" /
>
    </bList>
 </classC>


Can anyone tell me how to do this?
Thanks in advance...

Bonnie
0
12/10/2007 12:36:14 PM
dotnet.xml 7266 articles. 0 followers. Follow

0 Replies
586 Views

Similar Articles

[PageSpeed] 10

Reply:

Similar Artilces:

IE7 changes image MIME type
Trying to upload a PNG or JPG image to a website fails. IE7 has changed the MIME types from image/jpeg to image/pjpeg and image/png to image/x-png, neither of which is accepted by the site. (a) Why does IE do this? (b) Can I stop it from doing this? I read at http://msdn.microsoft.com/en-us/library/ms775147(VS.85).aspx When uploading files from the client to the Web server, Windows Internet Explorer uses FindMimeFromData to determine a MIME type. Where possible, FindMimeFromData returns hard-coded values from the list of Known MIME Types rather than relying on the MIME typ...

Design issue about which class a method should belong to
Hello! We have the two classes Person and Elevator. I just wonder method NewFloorRequest that set the floor that the elevator should go to which class should it belong to between Person and Elevator. I mean if I put it in class Elevator we can pass the floor that we want to go to as a parameter. Class Elevator has information about how many floor the elevator has which mean the method can check if the passed floor for NewFloorRequest is valid. On the other hand if I put it in class Person which doesn't seem right but if I still put it in class Person we can return the ...

Changing Color Attributes for a Control Dynamically
Looking for help with a color setting on a report. I'm designing a report in which the background & foreground colors for a single control need to change based on a database / query produced value. Each record in the query contains the background & foreground numbers that I want a specific control to use when printed. For example, green = 44544. How can I get ACCESS to change these attributes dynamically? My database is running in Access 2000. Thanks in advance for any help. I had it figured out approx 2 years ago, and forgot how I did it! This "getting old thing&qu...

my latest C++ excel OLE automation code
// fm2n_old.cpp // The OLE Automation interface to Excel is mostly written using: // "How to automate Excel from C++ without using MFC or #import" // http://support.microsoft.com/kb/216686 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <windows.h> #include <comdef.h> #include "ole2.h" #include <ctype.h> #include <shellapi.h> #include "asstring.h" #include "fm2n_old.h" // main pointer for Excel IDispatch * pExcelApplication = NULL; // Workbooks collection IDispatch * pExcelWor...

Asynchronous HTTP class
I'm using VC++ 6.0, and I've heard bad things about WinInet in asynchronous mode. Specifically, that it isn't reliable. Is there another class I can use that would relieve me from having to worry about HTTP intricacies? You can use async socket classes of MFC e.g. Arkady "Jo" <JoJoTwilligo@hotmail.com> wrote in message news:1135017765.129844.315230@g14g2000cwa.googlegroups.com... > I'm using VC++ 6.0, and I've heard bad things about WinInet in > asynchronous mode. Specifically, that it isn't reliable. Is there > another class I can use that ...

Transfer Between Classes
I have a checking account, in which I've used Classification 1 and called it Checking. I've established classes, under Checking, named savings, emergency, cash. I want to transfer from one class to another in the checking account. However, when attempting to do this, I receive an error that I can't transfer from/to the same account, even though the classifications are different. Any idea as to how this is done? You can't it and it probably isn't sensible to try. That's like Transferring between, say, Food:Groceries and Automobile:Gasoline. If you classified a ...

Serial Number Problem
Hi Everyone, we run Dynamics on our server anyway we had HUGE problems and lost a lot of work.....anyway after fixing lots up i still have one MAJOR problem. Serial numbers for items in the system do exist if you look up inventory ~ Serials. There is a list of serial numbers in stock ready to use. But when we go to do an invoice there are no serial numbers, it asks you if you want to overide the quantities... Our problem was we lost our inventory file therefore we had to put an old file back on...although these serial numbers are there we can not use them which also means i cant put t...

where to find doc. for Microsoft defined types
Is there a place where all the Microsoft data type definitions can be found (e.g. BOOL, Hwnd, UINT , etc. etc. . . .)? without having to hunt through all the .h files? My most recent snag: in a dialog box, I have class wizard define a member value variable for a checkbox, which shows up as a BOOL. In my code, I stored it in a variable defined as bool. Of course, this gives a warning, so I change mine to BOOL and everythings ok. But I would really like to know what BOOL is - so far, I haven't found it, but there are so many .h files ....... TIA for any guidance. ...

how to load a jpg using C
hi, how to load an iamge of jpg or tif can i load the jpg image using C code. if it possible pls mention any site that gives the related stuff. thanking you prasad Hi, I am a very happy user of NexgenIPL, which can be found through http://www.binary-technologies.com/index2.html It is a free VC library that supports many, many graphic formats. By the way... I have no money on them! ;-) Gr., Sander Verhagen [ Verhagen@nonono.Sander.com ] "prasad" <pppp23@hotmail.com> wrote in message news:54da01c376d2$8d8eedb0$a601280a@phx.gbl... > hi, > > how to load...

How to get polynome (or other type) coefficients of a trendline?
...

Duplicate Element Names in Serialized XML
Hi All, Hoping someone can solve a problem for me. I am modifying the schema for a rather complex web service written in VB.NET. The services uses a large number of serialized classes to output the overall XML. The problem I am having is that I require 2 XML elements of the same name to be serialized from the 1 class. Due to the way the web service is written only 1 of these would be ever be called at a time. However, it seems that I get an exception if the same element name is used twice in one class.. any ideas? For example: 'start class here <XMLElement ("elementName&q...

Short date format in strongly typed dataset date column.
Is there a way to save date data in a short format rather than the long form which a strongly typed dataset saves it which looks like this: d3p1:WeekEnding="2003-02-02T00:00:00.0000000-06:00". I would like to shorten it like this: ="2003-02-02" Thanks. -- George Padvorac georgep@dontspamme-nwis.net ...

-> 2003: Plain text wrapping as I type? <-
Hi, I'm using Outlook 2003 and have set the auto wrapping to 76 for my plain text messages. At work, they autowrap as I type. At home I can type miles and miles of text without wrapping appearing in real time. It's wrapped only when sending the message. What's wrong? I'd like Outlook 2003 to wrap text as I type so I can know how the message will look like before sending it. I have applied all the patches. Any clue why I get the correct behavior at work (where I have Office 2003) and home (where I only have Outlook, without MS Office)??? Thanks for any help. Sincerely, St...

Serial Motor
How do you control a motor via serial port? What language should I use? Justin Case wrote: > How do you control a motor via serial port? What language should I > use? I don't know, but it certainly isn't possible with SQL Server ... http://lmgtfy.com/?q=How+do+you+control+a+motor+via+serial+port -- HTH, Bob Barrows Bob Barrows (reb01501@NOyahoo.SPAMcom) writes: > I don't know, but it certainly isn't possible with SQL Server ... What? You can't control a motor with an engine? -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se...

GUI/MFC/C++ developer position in London, UK
The company, Biotronics3D, based in East London,UK is a new and dynamic venture dealing in advanced technologies. We market novel and innovative solutions to the medical international market. It is created by a group of scientists and businessmen, with ample experience in this market sector. The patented technology allows doctors to improve their diagnostic efficiency and accuracy via a set of software and hardware tools. We have many strategic partnerships with Universities and Hospitals. Our current strategy is to expand our research and development activities by fusing technologies from the...

serial port communuication
Hi all, I m communicating to a printer device through serial port.It is failing to open the port,but when I disable and enable the com port in device manager,it works fine(i.e)it communicates with the printer and reads the configuration settings of the printer). Regards Hari are u leaving the port opened when ur program terminates ........ what is the frequency of u disabling/enabling port ....... after every run ...... some times works some times not ..... ????? "hari" <haricibi83@gmail.com> wrote in message news:1171455246.402743.203120@k78g2000cwa.googlegrou...

Find as u type by Allen Browne question
I've used the 'find as u type' utility (http://allenbrowne.com/AppFindAsUType.html) many times successfully, but I'm now running into something I can't solve. When I base the form on a table or selection query, everything works fine. But when I base the form on a query which in itself is based on a query and a linked table I get the following behavior: 1. The form_load works fine. 2. Filtering works fine as long as the value is found. 3. When the typed value can't be found the record fields disappear and the focus is set to the field where you enter the value (th...

Serialization of jagged Arrays Issue
Hi there, I m trying to return an object (of my own written class) from a web service that contains jagged Arrays as public variables. Asp.Net is showing me the its serialized version on the browser when i invoke the service during test. Code: public class returnType { [System.Xml.Serialization.XmlElementAttribute("chassisdata")] public _chassisdata[] chassisdata; [XmlArray(),XmlArrayItem("wheel", typeof(_wheel[]),IsNullable=false)] public _wheel[][] upsteps; [XmlArray(),XmlArrayItem("wheelsize", typeof(_wheelsize[]),IsNullable=false)] publi...

No text converter is installed for this file type
This is a multi-part message in MIME format. ------=_NextPart_000_0009_01CA6771.CB83FA70 Content-Type: text/plain; charset="big5" Content-Transfer-Encoding: quoted-printable Hi Expert=20 I am now facing a problem in opening a ppt in Powerpoint 2003, an error = display as follow: "PowerPoint can't read the outline from filename. No text converter is = installed for this file type." My platform status: 1. Windows XP Professional SP3 2. Microsoft Office 2003 SP3 3. Office 2007 Compatibility Pack installed Who can help me? TKNL __...

open files serially
Hi, I seek your help for the following; I have few files in one of the folder. The file names are ending with numeric number. The uniqueness of the file names are they are serially numbered e.g. ABC123.xls, XYZ124.xls, MNO_AB125.xls, LKM126.xls. I would like to open these files serially (...123 then ...124 so on so forth till last files in the folder). Once the file is open i would like to do some processing and save it in another folder. The files once opened and saved in another folder it should get deleted from earlier folder (From where the file was opened). Than...

how do I type over a pic on excel
Unless you put the picture in the Background (Format/Sheet/Background), in which case it will be tiled throughout the entire sheet, you can't directly type over a picture because pictures "live" in the Drawing layer above the cells. You can make the picture transparent (Format/Picture) to see text in the cells beneath. You can also use the Camera Tool to create a linked picture of a cell and position it on top of the other picture. Set the Fill to No fill, and the border to No Line. What I do is to put a text box over the picture and format it with n lines and no fill...

COleDateTimeSpan
I noticed problem in Visual Studio.NET 7.0.9466, don't know if something wrong with my VC source code. the bug could cause data corruption if you use serialization of COleDatTimeSpan object, the problem is in file olevar.cpp, one line of code is missing (at least on my mashine ============= CArchive& AFXAPI operator<<(CArchive& ar, COleDateTimeSpan dateSpanSrc return ar << dateSpanSrc.m_span CArchive& AFXAPI operator>>(CArchive& ar, COleDateTimeSpan& dateSpanSrc ar >> (long&)dateSpanSrc.m_status return ar >> dateSpanSrc.m_span ...

Serialize
Hi All, I use Serialize() to open files in my application. However, if the file is ReadOnly I get the error "Failed to open document" from CDocManager::OpenDocumentFile(LPCTSTR lpszFileName). Anyone have any ideas how to get around this? Cheers, Rob ...

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> ...

No function definitions in MFC project generated ActiveX control wrapper class
I have a dialog based MFC project created in Visual Studio 2003 environment. I inserted an ActiveX control object into a dialog and tried to created a variable for the control. But I don't see all the interface method definitions in the MFC generated wrapper class, so I cannot call those methods through the variable. I just added a new method to the control. I remembered it worked fine before. Does anyone have any idea why is that? Any help would be much appreciated! ...