By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,482 Members | 3,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,482 IT Pros & Developers. It's quick & easy.

Using XmlRoot attribute for Xml Serialization only works for thefirst element

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.