Pregunta

I'm trying to do deserialize XML to model. I have, for example:

<A>
    <B id=1></B>
    <B id=2></B>
    <B id=3></B>
</A>

Is it possible to grab id's of B without creating model B? So I want to have something like this in the end:

public class A {
    [XmlAttribute(B/@id)]
    public List<int> ids { get; set; }
}

Thx.

¿Fue útil?

Solución

You can implement the ISerializable interface to add custom serialization / deserialization.

http://msdn.microsoft.com/en-us/library/ty01x675.aspx

[Serializable]
public class MyObject : ISerializable 
{
  public int n1;
  public int n2;
  public String str;

  public MyObject()
  {
  }

  protected MyObject(SerializationInfo info, StreamingContext context)
  {
    n1 = info.GetInt32("i");
    n2 = info.GetInt32("j");
    str = info.GetString("k");
  }
[SecurityPermissionAttribute(SecurityAction.Demand, 
SerializationFormatter =true)]

public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
  {
    info.AddValue("i", n1);
    info.AddValue("j", n2);
    info.AddValue("k", str);
  }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top