0

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 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Don't forget about [XmlRoot(ElementName="N")] for the MyN class
Thank you. It works. But there were some problems with array serialization. XmlArrayItem and XmlArray attributes solve it.
3
[XmlRoot(ElementName="N")]
public class MyN
{
    [XmlAttribute]
    public string Name { get; set; }
    public string Number { get; set; }
}

1 Comment

This will assume that you have <N Name="MyName"><Number>Number of MyName</Number></N>
0
MyN
{
  [XmlAttribute(AttrName='Name')]
  public string Name {get;set;}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.