473,324 Members | 1,856 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,324 software developers and data experts.

XmlSerializer not creating attribute values

I wrote a simple function to serialize types. I create a simple struct,
marking its public properties with [XmlAttribute]. When I serialize this
struct, none of the properties are present as attributes in the xml stream.
public static string XmlSerialize( object target )
{
XmlSerializer serializer = new XmlSerializer( target.GetType() );
StringBuilder xml = new StringBuilder();
StringWriter writer = new StringWriter( xml );
serializer.Serialize( writer, target );
writer.Close();
return xml.ToString();
}
public struct MyStruct
{

private int? _id;
private string _name;

[XmlAttribute]
public int? Id
{
get { return _id; }
}

[XmlAttribute]
public string Name
{
get { return _name; }
}

public MyStruct( int id, string name )
{
_id = id;
_name = name;
}

}
MyStruct my = new MyStruct( 7, "test" );
string xml = XmlSerialize( my );
OUTPUT :

<?xml version="1.0" encoding="utf-16"?>
<MyStruct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
Nov 1 '08 #1
1 2288
John A Grandy wrote:
I wrote a simple function to serialize types. I create a simple struct,
marking its public properties with [XmlAttribute]. When I serialize this
struct, none of the properties are present as attributes in the xml stream.
public struct MyStruct
{

private int? _id;
private string _name;

[XmlAttribute]
public int? Id
{
get { return _id; }
}

[XmlAttribute]
public string Name
{
get { return _name; }
}

public MyStruct( int id, string name )
{
_id = id;
_name = name;
}

}
You will need set implemente din both properties.

And you will need to either use int instead of int? or
use an element instead of an attribute (attributes can not
be nullable).

Arne
Nov 2 '08 #2

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

Similar topics

8
by: Harris Boyce | last post by:
Hello, I'm trying to use the FOR XML EXPLICIT clause with SQL Server to deserialize data from my database into a strongly-typed collection object that I will use throughout my application. I...
4
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
0
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms...
5
by: Keith Bannister | last post by:
I'm new to .net so here goes. I'm tying to deserialize a class that is associated with an XML schema. I created the C# class with xsd.exe as below: xsd.exe /c /n:somenamespace...
3
by: stax | last post by:
Hello, can somebody tell me how to serialize/deserialize a object containing a multi line string using the XmlSerializer class. One of the both windows linefeed chars get dumped somewhere down...
1
by: SteveB | last post by:
I'm porting an application from Apache Xerces to .Net and am having a couple of small problems with deserialization. The XML that I'm reading comes from a variety of sources, and there are two...
10
by: Henrik Dahl | last post by:
Hello! I have an xml schema which has a date typed attribute. I have used xsd.exe to create a class library for XmlSerializer. The result of XmlSerializer.Serialize(...) should be passed as the...
1
by: =?Utf-8?B?S2llcmFuIENvdWdobGFu?= | last post by:
"Larry Bud" wrote: I had this error this morning. I had XML which looked like this: <ConstraintList> <Constraint> <Test .... > <Test .... > </Constraint>
2
by: butch77 | last post by:
Hi! We're creating flash websites, and we are now switching over to flash 9. We decided now to use webservices for the front end. And we have already been using webservices for the CMS. Now my...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.