C# XmlSerializer:反序列化 XML 文件中的空 GUID 值
C# XmlSerializer: Deserialize empty GUID value from XML file
我有一个包含 XML 的配置文件,如下所示:
<XmlParameter Title="Analysis">
<ProcedureName>XmlParameterAnalysisGetFirst</ProcedureName>
<ProcedureGroupId/>
<Returns>Table</Returns>
<Params>
<Param OrderKey="0" Name="xmlParameters">%xmlParameters%</Param>
</Params>
</XmlParameter>
我使用 XmlSerializer
class 在运行时将文件反序列化为 XmlParameter
:
类型的对象
public class XmlParameter {
[XmlAttribute]
public string Title { get; set; }
public string ProcedureName { get; set; }
[XmlElement(IsNullable=true)]
public Guid? ProcedureGroupId { get; set; }
public string Returns { get; set; }
public List<XmlCLRParam> Params { get; set; }
}
但是当我尝试使用此代码块反序列化文件时:
var deserializer = new XmlSerializer(typeof(XmlParameter);
var reader = new StreamReader(this.FilePath);
var obj = deserializer.Deserialize(reader);
我得到一个 System.InvalidOperationException
:
Error in XML-Document. ---> Sytem.Format.Exception: Unknown Guid-format.
我的问题是是否有办法反序列化 XML-file
中的 nullable
GUID 值
可空意味着如果 xml 文件中不存在,POCO 字段将获得一个空引用...
您的 xml 格式错误:删除此:
< ProcedureGroupId/>
我有一个包含 XML 的配置文件,如下所示:
<XmlParameter Title="Analysis">
<ProcedureName>XmlParameterAnalysisGetFirst</ProcedureName>
<ProcedureGroupId/>
<Returns>Table</Returns>
<Params>
<Param OrderKey="0" Name="xmlParameters">%xmlParameters%</Param>
</Params>
</XmlParameter>
我使用 XmlSerializer
class 在运行时将文件反序列化为 XmlParameter
:
public class XmlParameter {
[XmlAttribute]
public string Title { get; set; }
public string ProcedureName { get; set; }
[XmlElement(IsNullable=true)]
public Guid? ProcedureGroupId { get; set; }
public string Returns { get; set; }
public List<XmlCLRParam> Params { get; set; }
}
但是当我尝试使用此代码块反序列化文件时:
var deserializer = new XmlSerializer(typeof(XmlParameter);
var reader = new StreamReader(this.FilePath);
var obj = deserializer.Deserialize(reader);
我得到一个 System.InvalidOperationException
:
Error in XML-Document. ---> Sytem.Format.Exception: Unknown Guid-format.
我的问题是是否有办法反序列化 XML-file
中的nullable
GUID 值
可空意味着如果 xml 文件中不存在,POCO 字段将获得一个空引用...
您的 xml 格式错误:删除此:
< ProcedureGroupId/>