views:

125

answers:

3

Hi! I have a question about deserialization. It's a part of xml file

<N Name="MyName">Number of MyName</N>

and class in c#:

MyN
{
  [XmlAttribute(AttrName='Name')]
  public string Name {get;set;}

  public string Number {get;set}
}

I want to make that value of N in xml file (in samle - "Number of MyName") will deserialze in property Number of MyN class.

Thanks.

+3  A: 
[XmlRoot(ElementName="N")]
public class MyN
{
    [XmlAttribute]
    public string Name { get; set; }
    public string Number { get; set; }
}
Darin Dimitrov
This will assume that you have `<N Name="MyName"><Number>Number of MyName</Number></N>`
Regent
+5  A: 

Use [XmlText()] Attribute

[XmlRoot(ElementName="N")]
MyN
{
    [XmlAttribute(AttrName='Name')]
    public string Name {get;set;}
    [XmlText()]
    public string Number {get;set}
}

Check this for more information about Xml Serialization in C# http://www.dotnetjohn.com/articles.aspx?articleid=173

viky
Don't forget about `[XmlRoot(ElementName="N")]` for the `MyN` class
Regent
Thank you. It works. But there were some problems with array serialization. XmlArrayItem and XmlArray attributes solve it.
dekko
@Regent: I have updated my code!!
viky
A: 
MyN
{
  [XmlAttribute(AttrName='Name')]
  public string Name {get;set;}

  [XmlText]
  public string Number {get;set}
}
AZ