Using XmlRoot attribute for Xml Serialization only works for thefirst element

Y

Ympostor

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

Lebesgue

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 said:
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.
 
Y

Ympostor

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.

--
 
Y

Ympostor

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!

--
 
Top