[英]C# UniqueId class XML deserialization
我正在尝试从XML文件反序列化urn:uuid:a0a32a4f-9687-46ef-bdc7-e4ab049ac56f
我的文件样本是
<Document>
<Name>Billy</Name>
<TheId>urn:uuid:a0a32a4f-9687-46ef-bdc7-e4ab049ac56f</TheId>
</Document>
我正在尝试反序列化到以下课程
public class Document
{
public string Name { get; set; }
public UniqueId TheId { get; set; }
}
我的完整演示代码:
[Serializable]
public class Document
{
public string Name { get; set; }
public UniqueId TheId { get; set; }
}
class Program
{
static void Main(string[] args)
{
string Xml = @"
<Document>
<Name>Billy</Name>
<TheId>urn:uuid:a0a32a4f-9687-46ef-bdc7-e4ab049ac56f</TheId>
</Document>
";
XmlReader reader = XmlReader.Create(new StringReader(Xml));
XmlSerializer serializer = new XmlSerializer(typeof(Document));
Document doc = (Document)serializer.Deserialize(reader);
Console.WriteLine("Name is: {0}", doc.Name);
Console.WriteLine("Id is: {0}", doc.TheId);
Console.ReadKey();
}
}
但是每次我得到新的uniquid而不是存储在XML文档中的值。
我做错了什么?
UPD: UniqueId
类位于System.Runtime.Serialization程序集中
我的最终目标是将这个值添加到Guid
,我想将它与UniqueId::TryGetGuid
。
简单的解决方案:
public class Document
{
public string Name { get; set; }
[EditorBrowsable(EditorBrowsableState.Never)]
[XmlElement("TheId")]
public string TheIdString { get => TheId.ToString(); set => TheId = new UniqueId(value); }
[XmlIgnore]
public UniqueId TheId { get; set; }
}
添加一个将被序列化/反序列化的不同string
属性,并使它的支持“字段”成为UniqueId TheId
(我认为这是适配器模式,但不确定)。
通过查看UniqueId
的源代码,我看不到XmlSerializer
特殊处理,例如IXmlSerializable
接口。 也没有任何文档与XmlSerializer
兼容。
啊... [EditorBrowsable(EditorBrowsableState.Never)]
是这样,你是不是建议uniqueIdImpl
通过智能感知属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.