1

I have a problem controlling how the xml serializer formats a Guid.

is there anyway to force the property of the first example to exclude the dashes, using the XmlSerialzer?

example:

public class Example{
   [XmlElement]
   public Guid Value {get; set;}
}

when serializing the above class i would get: (36 characters)

<Example>
  <Value>3164fc09-1dc5-4629-b04c-e9cdc5e85de4</Value>
</Example>

but i want the value not to include dashes (32 characters)

<Example>
  <Value>06102471381242609d0176b269120082</Value>
</Example>

normally i would not care about the difference, but in this particular case i don't have a choice as i have to follow a standard provided by a third party that includes a set of xsd's.

I know i can do something like this to solve the problem, but it is messy and i would like to avoid it:

public class Example
{
    [XmlIgnore]
    public Guid Value { get; set; }
    [XmlElement(nameof(Value))]
    public string ValueString {
        get => Value.ToString("N");
        set
        {
            if (Guid.TryParse(value, out var uuid))
                Value = uuid;
            else
                throw new InvalidOperationException();
        }
    }
}

Provided xsd

the xsd provided by this company define the custom type to be a string with a range of 1 to 35 characters, but their documentation states that the value should always be a uuid version 4 with no dashes and 32 characters long.

the documentation provided only have a danish version.

See page 5 sekt 01.07.2016 UUID i EpisodeOfCareIdentifier for the specific problem.

about the standard (optional reading):

This standard is part of a message service between counties, hospitals and other independent healthcare specialists, regarding patient treatments. All in all there is quite a few software systems that have to implement this standard.

Jesper Rytter
  • 153
  • 1
  • 9
  • 1
    I don't think there's a better way to do this. The trick from [Most elegant XML serialization of Color structure](https://stackoverflow.com/a/4322461/3744182) doesn't seem to work here because `XmlSerializer` considers `Guid` to be a "primitive", see https://dotnetfiddle.net/GlVksm: *The type for XmlElement may not be specified for primitive types.* – dbc Apr 10 '20 at 19:08
  • Hi Jesper. Did you get the solution for this? I've stumbled in the same place. – Amrit Kr Lama Aug 20 '20 at 08:07

0 Answers0