473,554 Members | 2,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13372
"Jiho Han" <ji******@infin ityinfo.com> wrote in message news:#a******** ******@TK2MSFTN GP11.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,

- - - SerializingForm attedGuid.cs
[XmlRoot( ElementName="Se rializeFormatte dGuidExample")]
public class SerializingForm attedGuid
{
// 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.New Guid( ).ToString( "B").ToUppe r( );
}
public GuidContainer( System.Guid itsGuid) {
this.mGuid = itsGuid.ToStrin g( "B").ToUppe r( );
}
[XmlText]
public string Guid
{
get { return mGuid; }
set { mGuid = value; }
}
}

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

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

[XmlElement("Tre s")]
public int fieldThree;

public SerializingForm attedGuid( )
{
// 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******** ******@TK2MSFTN GP09.phx.gbl...
"Jiho Han" <ji******@infin ityinfo.com> wrote in message

news:#a******** ******@TK2MSFTN GP11.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,

- - - SerializingForm attedGuid.cs
[XmlRoot( ElementName="Se rializeFormatte dGuidExample")]
public class SerializingForm attedGuid
{
// 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.New Guid( ).ToString( "B").ToUppe r( );
}
public GuidContainer( System.Guid itsGuid) {
this.mGuid = itsGuid.ToStrin g( "B").ToUppe r( );
}
[XmlText]
public string Guid
{
get { return mGuid; }
set { mGuid = value; }
}
}

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

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

[XmlElement("Tre s")]
public int fieldThree;

public SerializingForm attedGuid( )
{
// 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
630
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 then used as a primary key in a database table. I have seen two occasions over the last 6 months where after a certain point in time the same...
1
2373
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 separate header which I include to my code. That's a standard windows header not modified. What is going on with the compiler?
2
10752
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 issue and not supported in languages as Java or C# - for good reasons. I have an object graph I want to persist using the XML serialization features...
3
2348
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 Guid.NewGuid().ToString().ToUpper().Replace("-","") I have a working copy of this type of class that will work generating an element with the output i have and the following is an...
1
4354
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 done this before but this time I have no idea why I am getting the error. Any ideas ?? THANKS ! <qcsttatus> <manual sourcerootfolder=""...
5
6654
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 @@IDENTITY to retrieve it. I want to try and mimic the functionality of
0
1139
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 that I want to serialize an instance of this type, pass it as a parameter to wp1 and then deserialize it. Let's say that type1 is a person´s info,...
1
1875
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 the following value for a unqiueidentifier variable {A00C9D36-CC20-475F-A285-777CC739199C}. If I use another random GUID the stored procedure...
2
1271
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 pair class to store key,value and i have its collection class to store values in "derived" class) Serialization of "derived" class object is...
0
7530
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6156
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5162
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3556
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2022
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1141
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
845
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.