473,385 Members | 1,397 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,385 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 15340
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.