469,336 Members | 5,442 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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 12802
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robert Chapman | last post: by
1 post views Thread by Ufit | last post: by
2 posts views Thread by Daniel Faensen | last post: by
3 posts views Thread by Mark | last post: by
1 post views Thread by andrewcw | last post: by
5 posts views Thread by Jason L James | last post: by
reply views Thread by Miguel RS | last post: by
1 post views Thread by SealedClassSingleton | last post: by
2 posts views Thread by hisabir | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.