C sharp - Serializacion de listas en XML

 
Vista:

Serializacion de listas en XML

Publicado por Juan (10 intervenciones) el 28/11/2006 15:55:11
Hola. Tengo un problemita.
Tengo definida estas clases con sus atributos correspondientes para generar un archivo xml mediante la serializacion.

[XmlRoot(ElementName = "pregunta")]
public class pregunta
{
[XmlAttribute(AttributeName = "TipoPreg")]
public String TipoPreg;
[XmlAttribute(AttributeName = "Pregunta")]
public String Pregunta;
[XmlAttribute(AttributeName = "NMaximo")]
public int NMaximo;
[XmlAttribute(AttributeName = "NMinimo")]
public int NMinimo;
[XmlAttribute(AttributeName = "AplicaOptional")]
public String AplicaOpcional;
[XmlAttribute(AttributeName = "Respuestas")]
public List<Respuestas> Respuestas;
[XmlAttribute(AttributeName = "IDPregunta")]
public int IDPregunta;
}
[XmlRoot(ElementName = "Respuestas")]
public class Respuestas
{
[XmlAttribute(AttributeName = "Texto")]
public String Texto;
[XmlAttribute(AttributeName = "IDRespuesta")]
public int IDRespuesta;

public Respuestas(String Texto, int IDRespuesta)
{
this.Texto = Texto;
this.IDRespuesta = IDRespuesta;
}
}

[XmlRoot(ElementName = "RespuestaEncuesta")]
public class RespuestasEncuesta
{
[XmlAttribute(AttributeName = "IDPregunta")]
public int IDPregunta;
[XmlAttribute(AttributeName = "IDRespuesta")]
public int IDRespuesta;

public RespuestasEncuesta(int IDPregunta, int IDRespuesta)
{
this.IDPregunta = IDPregunta;
this.IDRespuesta = IDRespuesta;
}
}
[XmlRoot(ElementName = "Encuesta")]
public class Encuesta
{
[XmlAttribute(AttributeName = "NPreguntas")]
public int NPreguntas;
[XmlAttribute(AttributeName = "Preguntas")]
public List<pregunta> preguntas;
}

Uso este metodo para serializar una variable Encuesta del tipo Encuesta que esta cargada y me dice que no puede serializar el tipo
XmlSerializer s = new XmlSerializer(typeof(Encuesta));
TextWriter w = new StreamWriter(@"c:\list.xml");
s.Serialize(w, Encuesta);
w.Close();

y me arroja esta excepcion
System.InvalidOperationException was unhandled
Message="There was an error reflecting type 'DeviceApplication1.Encuesta'."
Source="System.Xml"
StackTrace:
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel)
at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at DeviceApplication1.Form1.generarXML() in C:\Documents and Settings\jfernandez.JSILVA.001\Mis documentos\Visual Studio 2005\Projects\DeviceApplication1\DeviceApplication1\Form1.cs:line 282
at DeviceApplication1.Form1..ctor() in C:\Documents and Settings\jfernandez.JSILVA.001\Mis documentos\Visual Studio 2005\Projects\DeviceApplication1\DeviceApplication1\Form1.cs:line 313
at DeviceApplication1.Program.Main() in C:\Documents and Settings\jfernandez.JSILVA.001\Mis documentos\Visual Studio 2005\Projects\DeviceApplication1\DeviceApplication1\Program.cs:line 15

Alguna idea?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 158
Bronce
Ha disminuido 1 puesto en C sharp (en relación al último mes)
Gráfica de C sharp

RE:Serializacion de listas en XML

Publicado por Yamil Bracho (1164 intervenciones) el 29/11/2006 18:56:03
Chequea la propiedad InnerException para mas detalles del error...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Serializacion de listas en XML

Publicado por Juan (10 intervenciones) el 29/11/2006 19:15:58
Hola Yamil.
Ya lo logre arreglar. Es que hay un atributo de XML que es propio para los ArrayList. Por eso no serializaba. Una vez que se lo puse se arreglo.
Gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar