Xml serialization problem

  • Thread starter Thread starter Polo
  • Start date Start date
P

Polo

Hi,

I have two public enums types each defined in a class (PolarDiagram and
KDiagram) in a namespace (RTech.Graphic)
I try to serialize this class with XmlSerializer without success
A suggestion ?
Thank's
Polo

[Serializable()]
public class Template
{
[XmlAttribute()]
public RTech.Graphic.PolarDiagram.ViewMode View =
RTech.Graphic.PolarDiagram.ViewMode.HalfMode;
[XmlAttribute()]
public RTech.Graphic.KDiagram.ViewMode View2 =
RTech.Graphic.KDiagram.ViewMode.TwoSide;
}
 
Hi,

It works for me:

XmlSerializer ser = new XmlSerializer (typeof (Template));
Template template = new Template ();
ser.Serialize (new System.IO.StreamWriter (@"C:\test.xml"), template);
 
I confirm that it doesn't work at me !

If I remove one enum it works but with the two not (Note that the template
is not the the RTech.Graphic namespace)
A+
 
Hi Polo,
I don't know what stops your class from working. Does it have any
constructors? If so it must have a default constructor too. Try to be more
explicit when defining serialization options.

Here is a working sample of a serializable class with two enum values:
using System;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace Serialize
{
public enum Values{A,B}
public enum ValuesB{C,D}

[XmlRoot("TryIt")]
public class Test
{
private Values v1;
private ValuesB v2;

[XmlAttribute("ValueOne")]
public Values V1
{
get{return v1;}
set{v1=value;}
}

[XmlAttribute("ValueTwo")]
public ValuesB V2
{
get{return v2;}
set{v2=value;}
}
}

public class MainClass
{
public static void Main()
{
Test t=new Test();
t.V1=Values.A;
t.V2=ValuesB.C;

XmlSerializer serializer=new XmlSerializer(typeof(Test));
FileStream fs=new FileStream(string.Format("{0}/output.xml",
Application.StartupPath),
FileMode.Create, FileAccess.Write);
try
{
serializer.Serialize(fs,t);
}
finally
{
fs.Close();
}
}
}
}

This self contained little app will generate a file that contains this:
<?xml version="1.0"?>
<TryIt xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ValueOne="A"
ValueTwo="C" />

/Hugo
 
Yes It is a runtime error


Informations supplémentaires : Une erreur s'est produite lors de la
réflexion du type 'DocWin.Template'.
A error occurs in the
reflexion of type 'DocWin.Template'

A+
 
In fact I have default constructor
I have in a DLL named Graphic.dll
namespace RTech.Graphic
{
public class PolarDiagram
{
public enum ViewMode
{
....
}
....
}
public class KDiagram
{
public enum ViewMode
{
....
}
....
}
}

And I have a EXE
Using RTech.Graphic;
namespace DocWin
{
public class Template
{
[XmlAttribute()]
public RTech.Graphic.PolarDiagram.ViewMode View1 =
RTech.Graphic.PolarDiagram.ViewMode.HalfMode;
[XmlAttribute()]
public RTech.Graphic.KDiagram.ViewMode View2 =
RTech.Graphic.KDiagram.ViewMode.OneSide;
//If I remove this last line It works but with the two
definitions no
.....
}
}

Hugo Wetterberg said:
Hi Polo,
I don't know what stops your class from working. Does it have any
constructors? If so it must have a default constructor too. Try to be more
explicit when defining serialization options.

Here is a working sample of a serializable class with two enum values:
using System;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace Serialize
{
public enum Values{A,B}
public enum ValuesB{C,D}

[XmlRoot("TryIt")]
public class Test
{
private Values v1;
private ValuesB v2;

[XmlAttribute("ValueOne")]
public Values V1
{
get{return v1;}
set{v1=value;}
}

[XmlAttribute("ValueTwo")]
public ValuesB V2
{
get{return v2;}
set{v2=value;}
}
}

public class MainClass
{
public static void Main()
{
Test t=new Test();
t.V1=Values.A;
t.V2=ValuesB.C;

XmlSerializer serializer=new XmlSerializer(typeof(Test));
FileStream fs=new FileStream(string.Format("{0}/output.xml",
Application.StartupPath),
FileMode.Create, FileAccess.Write);
try
{
serializer.Serialize(fs,t);
}
finally
{
fs.Close();
}
}
}
}

This self contained little app will generate a file that contains this:
<?xml version="1.0"?>
<TryIt xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ValueOne="A"
ValueTwo="C" />

/Hugo

Hi,

I have two public enums types each defined in a class (PolarDiagram and
KDiagram) in a namespace (RTech.Graphic)
I try to serialize this class with XmlSerializer without success
A suggestion ?
Thank's
Polo

[Serializable()]
public class Template
{
[XmlAttribute()]
public RTech.Graphic.PolarDiagram.ViewMode View =
RTech.Graphic.PolarDiagram.ViewMode.HalfMode;
[XmlAttribute()]
public RTech.Graphic.KDiagram.ViewMode View2 =
RTech.Graphic.KDiagram.ViewMode.TwoSide;
}
 
Back
Top