C# 为Xml序列化错误提供元素名称
我试图弄清楚如何向Web API提供自定义错误消息,或者至少为XML post指定元素名称。目前,我得到的模型状态错误是 XML文档2、4中存在错误 此错误的内部异常提供了以下方面的更多信息: 字符串“false fds”不是有效的布尔值 我希望能够向用户返回更具体的内容,说明包含无效值的元素,而不是让他们搜索XML以确定该值存在的位置 以下是我发布的XML:C# 为Xml序列化错误提供元素名称,c#,xml,asp.net-mvc,asp.net-web-api,xml-serialization,C#,Xml,Asp.net Mvc,Asp.net Web Api,Xml Serialization,我试图弄清楚如何向Web API提供自定义错误消息,或者至少为XML post指定元素名称。目前,我得到的模型状态错误是 XML文档2、4中存在错误 此错误的内部异常提供了以下方面的更多信息: 字符串“false fds”不是有效的布尔值 我希望能够向用户返回更具体的内容,说明包含无效值的元素,而不是让他们搜索XML以确定该值存在的位置 以下是我发布的XML: <?xml version='1.0'?> <checkin> <checkinType>1
<?xml version='1.0'?>
<checkin>
<checkinType>1</checkinType>
<server>server1</server>
<notes>New Checkin</notes>
<productCheckins>
<ihsCheckin>
<vendor>IBM</vendor>
<make>HTTP Server</make>
<model></model>
<version>8.5.5.0</version>
<installLocation>/opt/IBM</installLocation>
<is64Bit>false fds</is64Bit>
</ihsCheckin>
</productCheckins>
</checkin>
以下是我试图转换为的类:
[XmlRoot("checkin")]
public class Checkin
{
[XmlElement("checkinTime")]
public DateTime CheckinTime { get; set; }
[XmlElement("checkType")]
public int CheckinType { get; set; }
[XmlElement("notes")]
public string Notes { get; set; }
[XmlElement("server")]
public string Server { get; set; }
[XmlArray("productCheckins")]
[XmlArrayItem("wasCheckin", typeof(WASCheckin))]
[XmlArrayItem("ihsCheckin", typeof(IHSCheckin))]
public List<ProductCheckin> ProductCheckins { get; set; }
}
public class ProductCheckin
{
[XmlElement("vendor")]
public string Vendor { get; set; }
[XmlElement("make")]
public string Make { get; set; }
[XmlElement("model")]
public string Model { get; set; }
[XmlElement("version")]
public string Version { get; set; }
[XmlElement("installLocation")]
public string InstallLocation { get; set; }
[XmlElement("is64Bit")]
public bool Is64Bit { get; set; }
}
基本上,我只想说,这个错误与IS64位元素有关,但除了手动解析XML之外,我还没有找到这样做的方法
<is64Bit>false fds</is64Bit>
您可以将其视为字符串:
然后单独处理。我有点同意:
<is64Bit>false fds</is64Bit>
您可以将其视为字符串:
然后单独处理。这可能不是最好的例子。我只是在寻找一种方法,向用户提供更详细的错误信息。另一种情况可能是,用户将其中一个元素拼写错误或传递了一个不存在的元素。我知道他们可以自己验证,但我也希望能够向用户提供尽可能多的信息。实现我自己的序列化程序是更好的选择吗?@JStinebaugh框架中提供了一个xsd验证xml读取器,并提供了详细的输出。我会通过它来运行它。这可能不是最好的例子。我只是在寻找一种方法,向用户提供更详细的错误信息。另一种情况可能是,用户将其中一个元素拼写错误或传递了一个不存在的元素。我知道他们可以自己验证,但我也希望能够向用户提供尽可能多的信息。实现我自己的序列化程序是更好的选择吗?@JStinebaugh框架中提供了一个xsd验证xml读取器,并提供了详细的输出。我会通过它来运行它。
[XmlElement("is64Bit")]
public string Is64Bit { get; set; }