Serialize complex type element with simple content

Discussion in 'ASP .Net Web Services' started by Albert Jan, Oct 4, 2004.

  1. Albert Jan

    Albert Jan Guest

    Hi,

    This must be easy, but I can't find an example:

    I want to automatically serialize an element with an attribute like the
    following
    <ClientSoftware version="(254)">MySoftware</ClientSoftware>

    to a C# class.

    If 'Clientsoftware' has child elements I can use something like the code
    given below , but I don't know how to do this when the element has simple
    content.

    I appreciate any help on this subject,

    Albert Jan




    using System;
    using System.Xml.Serialization;
    namespace myns
    {
    public class ClientSoftwareType
    {
    virtual public System.String Name
    {
    get
    {
    return name
    }

    set
    {
    name = value;
    }

    }

    [XmlAttribute ("version")]
    virtual public System.String Version
    {
    get
    {
    return version;
    }

    set
    {
    this.version = value;
    }

    }
    private System.String name;
    private System.String version;
    }
    }
    Albert Jan, Oct 4, 2004
    #1
    1. Advertising

  2. public class ClientSoftwareType
    {
    [XmlAttribute]
    public string Version

    [XmlText]
    publc string Value
    }

    You can use properties instead of public fields, just be sure to use the
    [XmlText] attribute, that is what you are looking for.

    "Albert Jan" wrote:

    > Hi,
    >
    > This must be easy, but I can't find an example:
    >
    > I want to automatically serialize an element with an attribute like the
    > following
    > <ClientSoftware version="(254)">MySoftware</ClientSoftware>
    >
    > to a C# class.
    >
    > If 'Clientsoftware' has child elements I can use something like the code
    > given below , but I don't know how to do this when the element has simple
    > content.
    >
    > I appreciate any help on this subject,
    >
    > Albert Jan
    >
    >
    >
    >
    > using System;
    > using System.Xml.Serialization;
    > namespace myns
    > {
    > public class ClientSoftwareType
    > {
    > virtual public System.String Name
    > {
    > get
    > {
    > return name
    > }
    >
    > set
    > {
    > name = value;
    > }
    >
    > }
    >
    > [XmlAttribute ("version")]
    > virtual public System.String Version
    > {
    > get
    > {
    > return version;
    > }
    >
    > set
    > {
    > this.version = value;
    > }
    >
    > }
    > private System.String name;
    > private System.String version;
    > }
    > }
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    tom wisnowski, Oct 5, 2004
    #2
    1. Advertising

  3. Albert Jan

    Albert Jan Guest

    Thank you Tom,

    indeed that is what I was looking for!

    Albert Jan

    "tom wisnowski" <> wrote in message
    news:...
    > public class ClientSoftwareType
    > {
    > [XmlAttribute]
    > public string Version
    >
    > [XmlText]
    > publc string Value
    > }
    >
    > You can use properties instead of public fields, just be sure to use the
    > [XmlText] attribute, that is what you are looking for.
    >
    > "Albert Jan" wrote:
    >
    > > Hi,
    > >
    > > This must be easy, but I can't find an example:
    > >
    > > I want to automatically serialize an element with an attribute like the
    > > following
    > > <ClientSoftware version="(254)">MySoftware</ClientSoftware>
    > >
    > > to a C# class.
    > >
    > > If 'Clientsoftware' has child elements I can use something like the code
    > > given below , but I don't know how to do this when the element has

    simple
    > > content.
    > >
    > > I appreciate any help on this subject,
    > >
    > > Albert Jan
    > >
    > >
    > >
    > >
    > > using System;
    > > using System.Xml.Serialization;
    > > namespace myns
    > > {
    > > public class ClientSoftwareType
    > > {
    > > virtual public System.String Name
    > > {
    > > get
    > > {
    > > return name
    > > }
    > >
    > > set
    > > {
    > > name = value;
    > > }
    > >
    > > }
    > >
    > > [XmlAttribute ("version")]
    > > virtual public System.String Version
    > > {
    > > get
    > > {
    > > return version;
    > > }
    > >
    > > set
    > > {
    > > this.version = value;
    > > }
    > >
    > > }
    > > private System.String name;
    > > private System.String version;
    > > }
    > > }
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    Albert Jan, Oct 5, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gordz
    Replies:
    3
    Views:
    3,921
    John Oakes
    Jun 7, 2004
  2. Ulf Heyder
    Replies:
    1
    Views:
    479
    Patrick TJ McPhee
    Oct 28, 2003
  3. Chishun Kwong
    Replies:
    0
    Views:
    406
    Chishun Kwong
    Mar 3, 2005
  4. Stanimir Stamenkov
    Replies:
    2
    Views:
    731
    Stanimir Stamenkov
    Oct 25, 2005
  5. Robert Mark Bram
    Replies:
    0
    Views:
    670
    Robert Mark Bram
    Feb 4, 2007
Loading...

Share This Page