473,398 Members | 2,368 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,398 software developers and data experts.

Xml Serialization - Guid problem...

I generated a class from a schema. One of the fields are typed as
System.Guid. Perfect.
The only problem is when this class serializes, the guid field serializes as
8c4a969b-2aa4-4679-b170-d9f6441f7c6d, when I need it as
{8A3ACA06-A7DE-41A5-B584-063E7CF391BB}, all upper, and with braces.

Is there a way to force the XmlSerializer to serialize as the latter format?

I tried to turn the field into a string property with private field type
Guid. That way, I can store it as a guid but format it the way I want. The
problem this time - the properties serialize last and my elements are now
out of order.

Is there a way to force a certain order in serialization?

I am looking at a few options:

1. turn all fields into properties.

2. write a custom XmlWriter which will replace all guid fields into the
correct format upon serializing.

3. write a custom XmlReader which will replace all guid fields into the
correct format upon deserializing.

4. use xsl transformation.

It seems to me that option #1 is the easiest to implement but that would
mean my class cannot be generated automatically any more.

Any other suggestions?
Thanks much.
Jiho
Nov 12 '05 #1
2 13348
"Jiho Han" <ji******@infinityinfo.com> wrote in message news:#a**************@TK2MSFTNGP11.phx.gbl...
The only problem is when this class serializes, the guid field serializes as
8c4a969b-2aa4-4679-b170-d9f6441f7c6d, when I need it as
{8A3ACA06-A7DE-41A5-B584-063E7CF391BB}, all upper, and with braces. : : Any other suggestions?


Option 5, instead of wrapping the formatting logic within a property,
wrap it within a nested class. Here is an example,

- - - SerializingFormattedGuid.cs
[XmlRoot( ElementName="SerializeFormattedGuidExample")]
public class SerializingFormattedGuid
{
// This is the nested class, containing a property that manages
// the formatting of the GUID.
public class GuidContainer
{
private string mGuid;
public GuidContainer() {
this.mGuid = System.Guid.NewGuid( ).ToString( "B").ToUpper( );
}
public GuidContainer( System.Guid itsGuid) {
this.mGuid = itsGuid.ToString( "B").ToUpper( );
}
[XmlText]
public string Guid
{
get { return mGuid; }
set { mGuid = value; }
}
}

[XmlElement("Uno")]
public int fieldOne;

[XmlElement("Dos")]
public GuidContainer guid;

[XmlElement("Tres")]
public int fieldThree;

public SerializingFormattedGuid( )
{
// Optionally, pass as an argument the specific GUID
// you wish to assign.
//
guid = new GuidContainer( );
fieldOne = 1;
fieldThree = 3;
}
}
- - -

It cries out "hack," of course. What would be nice would be a way to
specify an IFormatProvider, no?
Derek Harmon
Nov 12 '05 #2
Interesting approach. That still involved editing of the source generated
by xsd so I might as well make them properties.

I can't wait for version 2.0 of the framework... sigh...

"Derek Harmon" <lo*******@msn.com> wrote in message
news:uU**************@TK2MSFTNGP09.phx.gbl...
"Jiho Han" <ji******@infinityinfo.com> wrote in message

news:#a**************@TK2MSFTNGP11.phx.gbl...
The only problem is when this class serializes, the guid field serializes as 8c4a969b-2aa4-4679-b170-d9f6441f7c6d, when I need it as
{8A3ACA06-A7DE-41A5-B584-063E7CF391BB}, all upper, and with braces.

: :
Any other suggestions?


Option 5, instead of wrapping the formatting logic within a property,
wrap it within a nested class. Here is an example,

- - - SerializingFormattedGuid.cs
[XmlRoot( ElementName="SerializeFormattedGuidExample")]
public class SerializingFormattedGuid
{
// This is the nested class, containing a property that manages
// the formatting of the GUID.
public class GuidContainer
{
private string mGuid;
public GuidContainer() {
this.mGuid = System.Guid.NewGuid( ).ToString( "B").ToUpper( );
}
public GuidContainer( System.Guid itsGuid) {
this.mGuid = itsGuid.ToString( "B").ToUpper( );
}
[XmlText]
public string Guid
{
get { return mGuid; }
set { mGuid = value; }
}
}

[XmlElement("Uno")]
public int fieldOne;

[XmlElement("Dos")]
public GuidContainer guid;

[XmlElement("Tres")]
public int fieldThree;

public SerializingFormattedGuid( )
{
// Optionally, pass as an argument the specific GUID
// you wish to assign.
//
guid = new GuidContainer( );
fieldOne = 1;
fieldThree = 3;
}
}
- - -

It cries out "hack," of course. What would be nice would be a way to
specify an IFormatProvider, no?
Derek Harmon

Nov 12 '05 #3

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

Similar topics

3
by: Robert Chapman | last post by:
I know that this should be impossible, yet it is happening... I have some c# code that uses a method scoped variable of type Guid. The code assigns Guid.NewGuid() to the variable. This value is...
1
by: Ufit | last post by:
I can't figure it out - doing this DEFINE_GUID(CLSID_MYCL,......); I always get an error 'error LNK2001: unresolved external symbol _CLSID_MYCL' Why is that - I got lot's of GUIDs in...
2
by: Daniel Faensen | last post by:
As a good OO programmer that I hopefully am I prefer to implement against interfaces rather than classes. This is especially useful when it comes to multiple inheritance which is as you know an...
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...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
5
by: Jason L James | last post by:
Hi all, is there a method or property of a class that will return a GUID in VB.NET that I can use as the PK of my DB. I do not want SQL to generate it as I can not then use SELECT...
0
by: Miguel RS | last post by:
Hi all, I have a winforms app (app1), a webservice (ws1) and a webpage (wp1). The webservice exposes a type (type1). When I generate a proxy (px1) for app1 I get the type ws1.type1. The thing is...
1
by: SealedClassSingleton | last post by:
Hi, When calling a stored procedure on a SQL 2005 DB with the managed SQL ADOprovider I got the following exception: Syntax error converting character string to uniqueidentifier when I use...
2
by: hisabir | last post by:
Hi all I have some list serialization related problem. if u can help, I have a "base" class with some function A "derived" class that contains list object of about 17 length(I m using string...
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: 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...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.