Automatic XMLSerializing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hman

    #1

    Automatic XMLSerializing

    Hi,

    This is a two part question:

    1)trying to serialize a read only class member. I have a
    public property with a get statement, but not set, hence
    read only. When I serialize the class, this property is
    omitted. Can I serialze a public read-only member?

    Code segment:
    public class Page
    {
    private static int _number = 0;
    [XmlAttribute("n umber")]
    public int Number
    {
    get
    {
    return _number;
    }
    }

    }


    2)How do I change the element name of a class?
    I tried [XMLAttribute("P art")
    public class SectionPart
    {
    }

    but the compiler complained stating I couldn't do this.
    I wan't the serialization to change the <SectionPart>
    element to <Part>

    How is this done?

    Thanks a lot



  • Saurabh Nandu

    #2
    Re: Automatic XMLSerializing

    hi,

    Re 1: No You cannot serialize Read-Only properties.

    Re 2: You cannot apply the XmlAttribute to a class. It can only be applied
    to class members.

    Regards,
    Akila
    [MVP]


    Comment

    • hman

      #3
      Re: Automatic XMLSerializing


      If I use custom serialization, can these things be done?
      [color=blue]
      >-----Original Message-----
      >hi,
      >
      >Re 1: No You cannot serialize Read-Only properties.
      >
      >Re 2: You cannot apply the XmlAttribute to a class. It[/color]
      can only be applied[color=blue]
      >to class members.
      >
      >Regards,
      >Akila
      >[MVP]
      >
      >
      >.
      >[/color]

      Comment

      • Kalpesh Shah

        #4
        Re: Automatic XMLSerializing


        [XmlRootAttribut e(ElementName = "Part")]
        public class SelectionPart
        {
        ....
        }

        If the above doesnt work, try putting the following

        [XmlRootAttribut e(Namespace="ht tp://www.cpandl.com" ,
        ElementName = "Part", Form = XmlSchemaForm.Q ualified,
        IsNullable = false)]
        public class SelectionPart
        {
        ....
        }

        HTH
        Kalpesh

        Hman wrote:[color=blue]
        > Hi,
        >
        > This is a two part question:
        >
        > 1)trying to serialize a read only class member. I have a
        > public property with a get statement, but not set, hence
        > read only. When I serialize the class, this property is
        > omitted. Can I serialze a public read-only member?
        >
        > Code segment:
        > public class Page
        > {
        > private static int _number = 0;
        > [XmlAttribute("n umber")]
        > public int Number
        > {
        > get
        > {
        > return _number;
        > }
        > }
        >
        > }
        >
        >
        > 2)How do I change the element name of a class?
        > I tried [XMLAttribute("P art")
        > public class SectionPart
        > {
        > }
        >
        > but the compiler complained stating I couldn't do this.
        > I wan't the serialization to change the <SectionPart>
        > element to <Part>
        >
        > How is this done?
        >
        > Thanks a lot
        >
        >
        >[/color]


        Comment

        • Guest's Avatar

          #5
          Re: Automatic XMLSerializing


          Thanks for your response. What you recommended does not
          work. I already have a [XmlRoot("Survey ")] attribute
          defined for class Survey.

          Any other ideas?

          Thanks
          [color=blue]
          >-----Original Message-----
          >
          >[XmlRootAttribut e(ElementName = "Part")]
          >public class SelectionPart
          >{
          >....
          >}
          >
          >If the above doesnt work, try putting the following
          >
          >[XmlRootAttribut e(Namespace="ht tp://www.cpandl.com" ,
          >ElementName = "Part", Form = XmlSchemaForm.Q ualified,
          >IsNullable = false)]
          >public class SelectionPart
          >{
          >....
          >}
          >
          >HTH
          >Kalpesh
          >
          >Hman wrote:[color=green]
          >> Hi,
          >>
          >> This is a two part question:
          >>
          >> 1)trying to serialize a read only class member. I have[/color][/color]
          a[color=blue][color=green]
          >> public property with a get statement, but not set,[/color][/color]
          hence[color=blue][color=green]
          >> read only. When I serialize the class, this property[/color][/color]
          is[color=blue][color=green]
          >> omitted. Can I serialze a public read-only member?
          >>
          >> Code segment:
          >> public class Page
          >> {
          >> private static int _number = 0;
          >> [XmlAttribute("n umber")]
          >> public int Number
          >> {
          >> get
          >> {
          >> return _number;
          >> }
          >> }
          >>
          >> }
          >>
          >>
          >> 2)How do I change the element name of a class?
          >> I tried [XMLAttribute("P art")
          >> public class SectionPart
          >> {
          >> }
          >>
          >> but the compiler complained stating I couldn't do this.
          >> I wan't the serialization to change the <SectionPart>
          >> element to <Part>
          >>
          >> How is this done?
          >>
          >> Thanks a lot
          >>
          >>
          >>[/color]
          >
          >
          >.
          >[/color]

          Comment

          Working...