473,320 Members | 1,955 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,320 software developers and data experts.

Using XmlRoot attribute for Xml Serialization only works for thefirst element

Hello. I am using [XmlRoot()] attribute for redefining the element name
used in XML Serialization, but it only works for the parent elemnt:

Simple test code (referencing System.Xml):

using System;
using System.Collections.Generic;
using System.Text;

using System.Xml.Serialization;
using System.IO;

namespace XmlRootTest
{
[XmlRoot("This_works")]
public class MyList : List<MySecondList>{

private string myField;
public string MyField {
get { return this.myField; }
set { this.myField = value; }
}
}

[XmlRoot("This_does_not_work")]
public class MySecondList : List<MyClass>{
private string myField;
public string MyField {
get { return this.myField; }
set { this.myField = value; }
}
}

[XmlRoot("Neither_this_one")]
public class MyClass {

private string myField;
public string MyField {
get { return this.myField; }
set { this.myField = value; }
}
}

public class Program
{

public static void Main()
{

MyClass oMyObject = new MyClass();
oMyObject.MyField = "test4";

MyClass oMyObject2 = new MyClass();
oMyObject2.MyField = "test5";

MyClass oMyObject3 = new MyClass();
oMyObject3.MyField = "test6";
MyList oList = new MyList();
oList.MyField = "test";
MySecondList oList2 = new MySecondList();
oList2.MyField = "test2";

oList2.Add(oMyObject);
oList2.Add(oMyObject3);

MySecondList oList3 = new MySecondList();
oList3.MyField = "test3";
oList3.Add(oMyObject2);
oList3.Add(oMyObject);

oList.Add(oList2);
oList.Add(oList3);
XmlSerializer oSer = new XmlSerializer(typeof(MyList));
TextWriter oWriter = new StreamWriter("list.xml");
oSer.Serialize(oWriter, oList);
oWriter.Close();
}
}
}
Actual results:

<?xml version="1.0" encoding="utf-8"?>
<This_works xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfMyClass>
<MyClass>
<MyField>test4</MyField>
</MyClass>
<MyClass>
<MyField>test6</MyField>
</MyClass>
</ArrayOfMyClass>
<ArrayOfMyClass>
<MyClass>
<MyField>test5</MyField>
</MyClass>
<MyClass>
<MyField>test4</MyField>
</MyClass>
</ArrayOfMyClass>
</This_works>
Expected:
<?xml version="1.0" encoding="utf-8"?>
<This_works xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<This_does_not_work>
<Neither_this_one>
<MyField>test4</MyField>
</Neither_this_one>
<Neither_this_one>
<MyField>test6</MyField>
</Neither_this_one>
</This_does_not_work>
<This_does_not_work>
<Neither_this_one>
<MyField>test5</MyField>
</Neither_this_one>
<Neither_this_one>
<MyField>test4</MyField>
</Neither_this_one>
</This_does_not_work>
</This_works>

Does anybody know why? Thanks very much!

Regards.
Sep 4 '06 #1
3 15324
XmlRoot specifies the root name. Root is the upper most element. And there
is only a single root in xml file.
Maybe you wanted to use the XmlTypeAttribute, which would give you results
as you expected.

Use it like this:
....
[XmlType(TypeName = "This_does_not_work")]
public class MySecondList : List<MyClass>{
....

"Ympostor" <Ym******@NOSPAM-clix.ptwrote in message
news:eF**************@TK2MSFTNGP02.phx.gbl...
Hello. I am using [XmlRoot()] attribute for redefining the element name
used in XML Serialization, but it only works for the parent elemnt:

Simple test code (referencing System.Xml):

using System;
using System.Collections.Generic;
using System.Text;

using System.Xml.Serialization;
using System.IO;

namespace XmlRootTest
{
[XmlRoot("This_works")]
public class MyList : List<MySecondList>{

private string myField;
public string MyField {
get { return this.myField; }
set { this.myField = value; }
}
}

[XmlRoot("This_does_not_work")]
public class MySecondList : List<MyClass>{
private string myField;
public string MyField {
get { return this.myField; }
set { this.myField = value; }
}
}

[XmlRoot("Neither_this_one")]
public class MyClass {

private string myField;
public string MyField {
get { return this.myField; }
set { this.myField = value; }
}
}

public class Program
{

public static void Main()
{

MyClass oMyObject = new MyClass();
oMyObject.MyField = "test4";

MyClass oMyObject2 = new MyClass();
oMyObject2.MyField = "test5";

MyClass oMyObject3 = new MyClass();
oMyObject3.MyField = "test6";
MyList oList = new MyList();
oList.MyField = "test";
MySecondList oList2 = new MySecondList();
oList2.MyField = "test2";

oList2.Add(oMyObject);
oList2.Add(oMyObject3);

MySecondList oList3 = new MySecondList();
oList3.MyField = "test3";
oList3.Add(oMyObject2);
oList3.Add(oMyObject);

oList.Add(oList2);
oList.Add(oList3);
XmlSerializer oSer = new XmlSerializer(typeof(MyList));
TextWriter oWriter = new StreamWriter("list.xml");
oSer.Serialize(oWriter, oList);
oWriter.Close();
}
}
}
Actual results:

<?xml version="1.0" encoding="utf-8"?>
<This_works xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfMyClass>
<MyClass>
<MyField>test4</MyField>
</MyClass>
<MyClass>
<MyField>test6</MyField>
</MyClass>
</ArrayOfMyClass>
<ArrayOfMyClass>
<MyClass>
<MyField>test5</MyField>
</MyClass>
<MyClass>
<MyField>test4</MyField>
</MyClass>
</ArrayOfMyClass>
</This_works>
Expected:
<?xml version="1.0" encoding="utf-8"?>
<This_works xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<This_does_not_work>
<Neither_this_one>
<MyField>test4</MyField>
</Neither_this_one>
<Neither_this_one>
<MyField>test6</MyField>
</Neither_this_one>
</This_does_not_work>
<This_does_not_work>
<Neither_this_one>
<MyField>test5</MyField>
</Neither_this_one>
<Neither_this_one>
<MyField>test4</MyField>
</Neither_this_one>
</This_does_not_work>
</This_works>

Does anybody know why? Thanks very much!

Regards.

Sep 4 '06 #2
Lebesgue escribió:
XmlRoot specifies the root name. Root is the upper most element. And there
is only a single root in xml file.
Maybe you wanted to use the XmlTypeAttribute, which would give you results
as you expected.

Use it like this:
...
[XmlType(TypeName = "This_does_not_work")]
public class MySecondList : List<MyClass>{
...
I will try that, thanks!

Regards.

--

Sep 6 '06 #3
Ympostor escribió:
Lebesgue escribió:
>XmlRoot specifies the root name. Root is the upper most element. And
there is only a single root in xml file.
Maybe you wanted to use the XmlTypeAttribute, which would give you
results as you expected.

Use it like this:
...
[XmlType(TypeName = "This_does_not_work")]
public class MySecondList : List<MyClass>{
...

I will try that, thanks!
It worked! Thanks!

--
Sep 11 '06 #4

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

Similar topics

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...
0
by: Daniel Lidström | last post by:
Hi, for what reason do I put XmlType and XmlRoot attributes on classes for XML serialization? I understand the reason for the top element, but not why it is sometimes put for child elements. ...
1
by: Lena | last post by:
I have generated a proxy class derived from System.Web.Services.Protocols.SoapHttpClientProtocol One of my classes is defined as public class MyClass { public MyClass() {
3
by: Scott Liu | last post by:
HI, All, I have a web service doing a string search. It has an operator and a searchValue field. The operator is defined as an attribute and required. The xml is as below. <!--...
1
by: Massimo Bonanni | last post by:
Hi, I try to implement ASAP protocol in my web service, but I find a very hard problem. I define my SOAP Header: public class Request : SoapHeader {
1
by: Yewen Tang | last post by:
I have a schema file datamodel.xsd, element "properties" is declared as a type of "baseProperty". The schema file also defines "derivedProperty" is a derived type of "baseProperty". <?xml...
0
by: Ympostor | last post by:
Hello. I am using attribute for redefining the element name used in XML Serialization, but it only works for the parent elemnt: Simple test code (referencing System.Xml): using System; using...
4
by: kishore | last post by:
Hi I have a file similar to below <filter> <param id="xx">This is test</param> <param id="yy">This is another test </param> </filter> I created classes as below
9
by: Mark Olbert | last post by:
I'm trying to serialize (using XmlSerializer.Serialize) a class that I generated from an XSD schema using XSD.EXE /c. The problem I'm running into is that the root element needs to be unqualified,...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.