C# 在 XmlSerializer 中使用结构而不创建单独的 xml 节点

C# Using struct in XmlSerializer without creating a separate xml node

我有一个 class 我想序列化为一个 Xml:

[XmlRoot(ElementName = "readCase", Namespace = "XXX")]
public class ReadCase
{       
    [XmlElement(ElementName = "versionAsOf", Namespace = "XXX")]
    public BaseUtcTimeStamp VersionAsOf { get; set; }
}

我创建了一个结构 BaseUtcTimeStamp:

[Serializable]
public struct BaseUtcTimeStamp
{
    private string _utcTimestamp;

    public string UtcTimestamp { get => _utcTimestamp; set { } } // set is needed for XmlSerializer

    public BaseUtcTimeStamp(DateTime utcDateTime)
    {
        if (utcDateTime.Kind != DateTimeKind.Utc)
        {
            throw new ArgumentException("Given dateTime must be Utc.");
        }

        _utcTimestamp = utcDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
    }
 }

序列化后的 XML 如下所示:

<case:readCase>
   <case:versionAsOf>
     <case:UtcTimestamp>2021-07-01T07:38:14Z</case:UtcTimestamp>
   </case:versionAsOf>
 </case:readCase>

我希望 BaseUtcTimeStamp 类型值直接位于 VersionAsOf 中(如 GUID)。示例:

<case:readCase>
   <case:versionAsOf>2021-07-01T07:38:14Z</case:versionAsOf>
 </case:readCase> 
[XmlText]
public string UtcTimestamp { get => _utcTimestamp; set { } }