Guid xml序列化和验证

问题描述 投票:1回答:1

使用XmlSerializer序列化对象时,是否可以指定应如何将Guid属性格式化为字符串?

伪代码:

class MyObj {
    public Guid MyProp {get; set;}
}

序列化后得到的XML看起来像这样,(Guid没有花括号{})

<xml...>
  ..
  <MyProp>00000000-0000-0000-000000000000</MyProp>
  ..
</xml>

Microsoft在此处指定的Guid XSD:https://docs.microsoft.com/en-us/windows/win32/wes/eventschema-guidtype-simpletype

将大括号添加到Guid需要调用.ToString("B")

是否有办法告诉XmlSerializer如何使用特定的ToString格式格式化Guid?

xml serialization xsd format guid
1个回答
0
投票

通过执行以下操作使其正常工作:

[XmlIgnore]
public Guid MyProp { get; set; }

[XmlElement("MyProp")]
public string MyPropString
{
    get => MyProp.ToString("B");
    set => MyProp = Guid.Parse(value);
}
© www.soinside.com 2019 - 2024. All rights reserved.