473,382 Members | 1,390 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,382 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.

Jul 21 '05 #1
5 8003
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.

Jul 21 '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.

Jul 21 '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.


Jul 21 '05 #4

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.

Jul 21 '05 #5
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.

Jul 21 '05 #6

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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.