473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

XML Serialization Of A Class With Attributes

I am trying to serialize a class to xml and I havent been able to figure out
how to add attributes. I am trying to get a node to look like this

<alert type="pager" >True</alert>

I have this so far which produces this
<alert>True</alert>

[XmlElement("alert")]
public string Alert
{
get { return this.alert ; }
set { this.alert = value ; }
}

How would I embed an XML Attribute into that?

Thanks
Amy.

Nov 15 '05 #1
8 1622
have you tried [XmlElementAttribute("alert"] ?

regards
Oliver

"Amy L." <am**@paxemail.com> schrieb im Newsbeitrag
news:#z**************@tk2msftngp13.phx.gbl...
I am trying to serialize a class to xml and I havent been able to figure out how to add attributes. I am trying to get a node to look like this

<alert type="pager" >True</alert>

I have this so far which produces this
<alert>True</alert>

[XmlElement("alert")]
public string Alert
{
get { return this.alert ; }
set { this.alert = value ; }
}

How would I embed an XML Attribute into that?

Thanks
Amy.

Nov 15 '05 #2
sorry, I meant: have you tried [XmlAttribute("alert"]?

"Amy L." <am**@paxemail.com> schrieb im Newsbeitrag
news:#z**************@tk2msftngp13.phx.gbl...
I am trying to serialize a class to xml and I havent been able to figure out how to add attributes. I am trying to get a node to look like this

<alert type="pager" >True</alert>

I have this so far which produces this
<alert>True</alert>

[XmlElement("alert")]
public string Alert
{
get { return this.alert ; }
set { this.alert = value ; }
}

How would I embed an XML Attribute into that?

Thanks
Amy.

Nov 15 '05 #3
Oliver,

Yes I did, but it does not add it to the node "alert" instead it adds it to
the root.

Amy.
"Oliver Drobnik" <ne**@drobnik.com> wrote in message
news:OE**************@TK2MSFTNGP09.phx.gbl...
sorry, I meant: have you tried [XmlAttribute("alert"]?

"Amy L." <am**@paxemail.com> schrieb im Newsbeitrag
news:#z**************@tk2msftngp13.phx.gbl...
I am trying to serialize a class to xml and I havent been able to figure

out
how to add attributes. I am trying to get a node to look like this

<alert type="pager" >True</alert>

I have this so far which produces this
<alert>True</alert>

[XmlElement("alert")]
public string Alert
{
get { return this.alert ; }
set { this.alert = value ; }
}

How would I embed an XML Attribute into that?

Thanks
Amy.


Nov 15 '05 #4
Oliver,

Yes I did, but it does not add it to the node "alert" instead it adds it to
the root.

Amy.
"Oliver Drobnik" <ne**@drobnik.com> wrote in message
news:OE**************@TK2MSFTNGP09.phx.gbl...
sorry, I meant: have you tried [XmlAttribute("alert"]?

"Amy L." <am**@paxemail.com> schrieb im Newsbeitrag
news:#z**************@tk2msftngp13.phx.gbl...
I am trying to serialize a class to xml and I havent been able to figure

out
how to add attributes. I am trying to get a node to look like this

<alert type="pager" >True</alert>

I have this so far which produces this
<alert>True</alert>

[XmlElement("alert")]
public string Alert
{
get { return this.alert ; }
set { this.alert = value ; }
}

How would I embed an XML Attribute into that?

Thanks
Amy.


Nov 15 '05 #5

using System.Xml.Serialization;

public class Alert {

[XmlAttribute("Type")] // a named attribute
public string Type;
[XmlTextAttribute()] // the text attribute
public bool Status;

static void Main(string[] args) {
Alert a1 = new Alert();
a1.Type= "pager";
a1.Status= true;

XmlSerializer s = new XmlSerializer(typeof(Alert));

// use this to "suppress" the default xsd and xsd-instance namespaces
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add( "", "" );

// Serialize to stdout
s.Serialize(System.Console.Out,a1, ns);
}
}
-Dino
Microsoft

ps:
no need to cross post.
actually there is a more appropriate newsgroup:
microsoft.public.dotnet.xml
"Amy L." <am**@paxemail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I am trying to serialize a class to xml and I havent been able to figure out how to add attributes. I am trying to get a node to look like this

<alert type="pager" >True</alert>

I have this so far which produces this
<alert>True</alert>

[XmlElement("alert")]
public string Alert
{
get { return this.alert ; }
set { this.alert = value ; }
}

How would I embed an XML Attribute into that?

Thanks
Amy.

Nov 15 '05 #6

using System.Xml.Serialization;

public class Alert {

[XmlAttribute("Type")] // a named attribute
public string Type;
[XmlTextAttribute()] // the text attribute
public bool Status;

static void Main(string[] args) {
Alert a1 = new Alert();
a1.Type= "pager";
a1.Status= true;

XmlSerializer s = new XmlSerializer(typeof(Alert));

// use this to "suppress" the default xsd and xsd-instance namespaces
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add( "", "" );

// Serialize to stdout
s.Serialize(System.Console.Out,a1, ns);
}
}
-Dino
Microsoft

ps:
no need to cross post.
actually there is a more appropriate newsgroup:
microsoft.public.dotnet.xml
"Amy L." <am**@paxemail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I am trying to serialize a class to xml and I havent been able to figure out how to add attributes. I am trying to get a node to look like this

<alert type="pager" >True</alert>

I have this so far which produces this
<alert>True</alert>

[XmlElement("alert")]
public string Alert
{
get { return this.alert ; }
set { this.alert = value ; }
}

How would I embed an XML Attribute into that?

Thanks
Amy.

Nov 15 '05 #7
also! I forgot to mention, xsd.exe is a great tool for helping here.

1. create an xml file of the shape you think you want, eg
<root> <Alert type="pager">True</Alert></root>

2. run xsd.exe on that xml file, generate an xsd

3. run xsd.exe /c on that xsd file, generate a .cs file

4. view and modify the .cs file.

"Amy L." <am**@paxemail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I am trying to serialize a class to xml and I havent been able to figure out how to add attributes. I am trying to get a node to look like this

<alert type="pager" >True</alert>

I have this so far which produces this
<alert>True</alert>

[XmlElement("alert")]
public string Alert
{
get { return this.alert ; }
set { this.alert = value ; }
}

How would I embed an XML Attribute into that?

Thanks
Amy.

Nov 15 '05 #8
also! I forgot to mention, xsd.exe is a great tool for helping here.

1. create an xml file of the shape you think you want, eg
<root> <Alert type="pager">True</Alert></root>

2. run xsd.exe on that xml file, generate an xsd

3. run xsd.exe /c on that xsd file, generate a .cs file

4. view and modify the .cs file.

"Amy L." <am**@paxemail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I am trying to serialize a class to xml and I havent been able to figure out how to add attributes. I am trying to get a node to look like this

<alert type="pager" >True</alert>

I have this so far which produces this
<alert>True</alert>

[XmlElement("alert")]
public string Alert
{
get { return this.alert ; }
set { this.alert = value ; }
}

How would I embed an XML Attribute into that?

Thanks
Amy.

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
3
by: Mark | last post by:
Hello, What I am trying todo is use .NET serialization to take a Guid property that is an attribute in the XML document and have its output to be the same as...
16
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ..........
4
by: Jeff T. | last post by:
Hello, I have an existing set of C# classes that encapsulate our application data. They are in a heirachy with each subclass defining more specific types of data. I would like to serialize these...
2
by: Jesper Denmark | last post by:
Hi, I've had a lot of trouble finding a good tutorial showing deep serialization in XML in C#. All samples I've found shows merely serialization of a single class. If I want to serialize members...
5
by: George Ter-Saakov | last post by:
I have 2 classes one is document and another is collection of documents. When i am trying to use XmlSerializer to serialize class which has clsDcoumentIds member variable i am getting an exception...
2
by: Angelos Karantzalis | last post by:
Hi y'all, we're using a bunch of classes in a project, that we wanted to convert to-from xml easily. So, we defined XmlAttribute annotations for our class members and all worked fine. The Xml...
1
by: Rucha | last post by:
We are using ACAServices in our project, and are passing entity classes as parameters to the ACAServiceMethod. We are using a private variable entityState to indicate whether the entity is...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.