E Rolnicki
E Rolnicki

Reputation: 1717

.NET Xml Serialization: Integer Element with Attribute?

Is it possible to achieve the following in c#...

for the class below...

public class Foo{
 public int BarId{get;set;}
 public string BarString{get;set;}
}

I want to achieve the following XML:

<Foo>
  <BarId BarString="something">123</BarId>
</Foo>

Upvotes: 1

Views: 1924

Answers (2)

reSPAWNed
reSPAWNed

Reputation: 1099

ArsenMkrt is on the right track, but is missing the content of the element, I suggest a revised version:

class BarId
{
    [XmlText()]
    public int Content {get; set;}

    [XmlAttribute()]
    public string BarString {get; set;}
}

public class Foo{
    public BarId BarId {get; set;}
}

This way you get the content as an integer.

Upvotes: 4

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50752

You should create BarId class which has BarString in it

class BarId
{
    [XmlAttribute]
    public string BarString{get;set;}
}

public class Foo{
 public BarId BarId{get;set;}
}

Or you can use Custom Serialization mechanism like here

Upvotes: 0

Related Questions