Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# class to XML (xmlserializaion) problem

Tags:

c#

xml

Currently i am working with xml and have to populate xml file from C# object serializaion.Suppost below is the xml format what i want to do.

<DVD>
<Starring>
    <Star position="actor">
        Tom Hanks
    </Star>
   <Star position="actress">
        Robin Wright
    </Star>
<Title>Forrest Gump</Title>
</Starring>
</DVD>

I have two classes DVD and Star.

Public class DVD
{
  string title;
  public string Title{get{return title;}set {title=value;}}
  List<Star> Starring=new List<Star>();
}
public class Star
{
   string star;
   string pos;
   [XmlAttribute]
   public string Position{get{return pos;}set{pos=value;}}
   //Actually i don't want this property as a xmlelement
    public String StarName
   {
     get{return star;}
     set {star=value;} 
   }
}

Result from serialization is

<DVD>
<Starring>
    <Star position="actor">
        <StarName>Tom Hanks</StarName>
    </Star>
   <Star position="actress">
        <StarName>Robin Wright</StarName>
    </Star>
<Title>Forrest Gump</Title>
</Starring>
</DVD>

My problem is i don't find the way how to wrap Actor and Actress name without having StarName node.Is there a way to do this?.Any idea would be very appreciate.

like image 250
mtt Avatar asked Dec 13 '25 04:12

mtt


1 Answers

Use XmlTextAttribute on StarName property.

like image 92
baretta Avatar answered Dec 14 '25 18:12

baretta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!