Serialize complex type element with simple content

A

Albert Jan

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;
}
}
 
T

tom wisnowski

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.
 
A

Albert Jan

Thank you Tom,

indeed that is what I was looking for!

Albert Jan

tom wisnowski said:
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 said:
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;
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top