473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

XmlRootAttribute issue

Hi,

I created a strong-typed (Class1) collection class for my web service, it
actually serialized to <ArrayOfClass1 /> and I need to make the root named
as <Class1Collection /> so I was trying to add an XmlRootAttribute but I
always got error message when I tried to serialize this collection class,
Could anyone point me out why and how to achieve the goal?
[XmlRoot("Class1Collection")]
public class Class1Collection : Collectionbase
{
...
}

2. What should I do if I need the serialized xml has namespace prefix like
the following:

<ns0:Class1Collection xmlns:ns0="http://mynamespace">
...
</ns0:Class1Collection>
Thanks very much in advance!

John
Nov 17 '05 #1
5 9380
Hi John,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to add a root element for your
Xml serialization, and also add a namespace for the xml document. If there
is any misunderstanding, please feel free to let me know.

We can use the XmlRootAttribute and assign the Namespace and ElementName
properties to achieve that. Here is an example:

[XmlRoot(Namespace = "www.contoso.com", ElementName = "Class1Collection")]
public class Class1Collection : Collectionbase
{
//.....
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #2
Hi, Kevin,

Thanks for your reply but did you read my post? In my post, I stated when I
added the XmlRoot attribute and I got error when I serialize the object - I
knew we could use XmlRoot but I thought XmlRoot has some restriction on
certain type of class? Because I could serialize it before I added "XmlRoot"
attribute and it serialized to "<ArrayOfClass1 />" - after I added
XmlRoot("Class1Collection") then I could not use the same code to do the
serialization.

Thanks!
John

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:eA**************@TK2MSFTNGXA03.phx.gbl...
Hi John,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to add a root element for
your
Xml serialization, and also add a namespace for the xml document. If there
is any misunderstanding, please feel free to let me know.

We can use the XmlRootAttribute and assign the Namespace and ElementName
properties to achieve that. Here is an example:

[XmlRoot(Namespace = "www.contoso.com", ElementName = "Class1Collection")]
public class Class1Collection : Collectionbase
{
//.....
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #3
Hi John,

I think the problem you encountered is caused by the following things:

1. XmlRoot Attribute indicate that this class will be serizlied as the
RootElement of a XML document , however the Collection class is just a sub
element in the webservice SOAP message which can't be validated by the
runtime. So I think XmlRoot attribute can't be applied on your class.

2. As for changing the Serlized Element name of our custom Collection
class, after some local tests, I think we can't modify it directly if our
webmethod return the custom Collection class instance directly as return
type. This is because the ASP.NET webservice geneator will by default treat
such custom Collection class as a Array Type, ( you can verify this by
check the webservice 's webreference/ client proxy in client application),
the client side return Type is an Array of the ArrayItem's class rather
than the Collection class itself. And for Array Type, the .net will always
serilized it as "ArrayOf[ItemTypeName]" .

Currently I haven't found any direct means(Attribute) to modify the output
XML Element of an Array Type if we're using the default serlization of the
..net. However, we can workaround it through a wrapper class which contain
the Custom Collection class as a class member, then we can apply XmlElement
attribute on it to modify it's output ElementName, for example:

#we can specify attribute to customize the Wrapper element and the inner
collection property:
[XmlType("MyWrapper")]
public class Employees
{
private EmployeeCollection _items;

[XmlArray("MyEmployeeColletion")]
public EmployeeCollection Items
{
get
{return _items;}
set
{_items = value;}
}
}

public class EmployeeCollection:ICollection
{
.....
}

public class Employee
{
........
}

===========================

the output will be something like:
<MyWrapper xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">
<MyEmployeeColletion>
<Employee>
<EmpName>EmpName_0</EmpName>
<EmpID>EmpID_0</EmpID>
</Employee>
<Employee>
<EmpName>EmpName_1</EmpName>
<EmpID>EmpID_1</EmpID>
</Employee>
........................
</MyEmployeeColletion>
</MyWrapper>

=====================================
Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Nov 17 '05 #4
Thanks very much, Steven!

I understand your approach perfectly to have a wrapper class but the issue
is
the generated xml would have <MyWrapper> as its root - But I have to create
an object model that can produce the xml exactly as 3rd party's
specification which is

<Class1Collection>
<Class1 />
<Class1 />
</Class1Collection>

How could I create an object model (classes) that would produce the above
xml?

Thanks a lot!
John

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:$9**************@TK2MSFTNGXA03.phx.gbl...
Hi John,

I think the problem you encountered is caused by the following things:

1. XmlRoot Attribute indicate that this class will be serizlied as the
RootElement of a XML document , however the Collection class is just a sub
element in the webservice SOAP message which can't be validated by the
runtime. So I think XmlRoot attribute can't be applied on your class.

2. As for changing the Serlized Element name of our custom Collection
class, after some local tests, I think we can't modify it directly if our
webmethod return the custom Collection class instance directly as return
type. This is because the ASP.NET webservice geneator will by default
treat
such custom Collection class as a Array Type, ( you can verify this by
check the webservice 's webreference/ client proxy in client application),
the client side return Type is an Array of the ArrayItem's class rather
than the Collection class itself. And for Array Type, the .net will
always
serilized it as "ArrayOf[ItemTypeName]" .

Currently I haven't found any direct means(Attribute) to modify the output
XML Element of an Array Type if we're using the default serlization of the
net. However, we can workaround it through a wrapper class which contain
the Custom Collection class as a class member, then we can apply
XmlElement
attribute on it to modify it's output ElementName, for example:

#we can specify attribute to customize the Wrapper element and the inner
collection property:
[XmlType("MyWrapper")]
public class Employees
{
private EmployeeCollection _items;

[XmlArray("MyEmployeeColletion")]
public EmployeeCollection Items
{
get
{return _items;}
set
{_items = value;}
}
}

public class EmployeeCollection:ICollection
{
.....
}

public class Employee
{
.......
}

===========================

the output will be something like:
<MyWrapper xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">
<MyEmployeeColletion>
<Employee>
<EmpName>EmpName_0</EmpName>
<EmpID>EmpID_0</EmpID>
</Employee>
<Employee>
<EmpName>EmpName_1</EmpName>
<EmpID>EmpID_1</EmpID>
</Employee>
.......................
</MyEmployeeColletion>
</MyWrapper>

=====================================
Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 17 '05 #5
Glad to hear from you John!

I was just back from the holiday of the Labor Day :-). As for the further
question you mentioned, I think it's possible if what you want is just
build a class which can be serialized as the below output:
<myclassCollection xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<myclass id="0">Value_0</myclass>
<myclass id="1">Value_1</myclass>
<myclass id="2">Value_2</myclass>
</myclassCollection>
we can generate the above xml through the following class definition:
===================
public class myclassCollection
{

[System.Xml.Serialization.XmlElementAttribute("mycl ass",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public myclassCollectionMyclass[] Items;
}

public class myclassCollectionMyclass
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id;

[System.Xml.Serialization.XmlTextAttribute()]
public string Value;
}
=======================

The important thing is that we can't use our own custom Collection class,
but use a simple class which contains an Array member of the ArrayItem
class instances. In addition, I get the above class definition through the

xsd.exe tool. It's a quite good tool which can help auto-generate
XmlSchema definiation / class file through a given xml file. I think it'll
be helpful to you when you are wanting to create some class model for xml
serializaiton from some given xml template.

#XML Schema Definition Tool (Xsd.exe)
http://msdn.microsoft.com/library/en...chemadefinitio
ntoolxsdexe.asp?frame=true

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Nov 17 '05 #6

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

Similar topics

7
by: George Hester | last post by:
Please take a look at this google artcle: http://groups.google.com/groups?hl=en&lr=&frame=right&th=55d6f4b50f5f9382&seekm=411f370d%241%40olaf.komtel.net#link9 The op was having trouble with...
1
by: Dave | last post by:
Hello, I'm trying to solve an XML serialization problem that appears to me to be be a bug with the XmlSerializer. Let's say I have a class that looks like this: public class y { public...
10
by: Keith Patrick | last post by:
I've seen this come up before, but in my case, things are a little more complex, and I'm having a tough time figuring out how to set an element name that works. I have a configuration file that is...
2
by: Anthony Cuttitta Jr. | last post by:
We have an application that outputs several different graphs from data downloaded from our AS400. The application has worked without (this) issue for several months now, but just recently, the...
4
by: hazz | last post by:
I just haven't been able to get a solid feel for the purpose of what appears to be a declarative statement just above the class below. Anyone with a clear explanation that will finally stick with...
2
by: Ben Rush | last post by:
Hello World, Okay, I have spent the day browsing the newsgroups and reading up on article after article concerning ViewState corruption and so forth, and I have a couple questions. We...
0
by: Paul | last post by:
I have xml returning from a webservice which look like so... <CiscoIPPhoneMenu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">...
0
by: Vinny_Davis | last post by:
Hello - I have a complex object which I am exposing over a webservice on our intranet. In Visual studio 2003 I had to add "" just above my complex object in Reference.CS under Reference.map...
13
by: SAL | last post by:
Hello, I'm trying to include a popup in the ItemTemplate of a gridview row. The ItemTemplate for the field contains a textbox and when the user clicks in the textbox I want a popup panel to show...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.