输入字符串的格式不正确

本文关键字:不正确 格式 字符串 输入 | 更新日期: 2023-09-27 18:02:00

我正在尝试对经过XSD验证的XML文件进行反序列化。但问题是,其中一个节点可能不时是空的,所以我现在提出了这个XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="InterfaceLog">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Log" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="InterfaceID" type="xs:string"/>
        This line -->   <xs:element name="PrOrNumber" type="int-or-empty"/>
                        <xs:element name="MESKey" type="xs:string"/>
                        <xs:element name="MsgStatusCode" type="xs:string"/>
                        <xs:element name="MsgClass" type="xs:string"/>
                        <xs:element name="MsgNumber" type="xs:string"/>
                        <xs:element name="MsgDescription" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:simpleType name="int-or-empty">
    <xs:union memberTypes="xs:int empty-string" />
</xs:simpleType>
<xs:simpleType name="empty-string">
    <xs:restriction base="xs:string">
        <xs:enumeration value="" />
    </xs:restriction>
</xs:simpleType>
</xs:schema>

PrOrNumber行有时可能是空的,所以我想到了simpleType int或empty。因此,现在当我尝试反序列化XML时,它在尝试反序列化PrOrNumber时失败(返回错误输入字符串的格式无效(。

我正在尝试反序列化的XML

<?xml version="1.0" encoding="utf-8" ?>
<ns2:InterfaceLog xmlns:ns2="foo.com/foo">
<Log>
<InterfaceID>InterFace_Id</InterfaceID>
<PrOrNumber/>
<MESKey>Run</MESKey>
<MsgStatusCode>S</MsgStatusCode>
<MsgClass>Class</MsgClass>
<MsgNumber>123</MsgNumber>
<MsgDescription>Description</MsgDescription>
</Log>
</ns2:InterfaceLog>

以及我试图反序列化为的类

    [XmlRoot("InterfaceLog", Namespace = "foo.com/foo")]
[Serializable]
public class InterfaceLog
{
    [XmlElement("Log", Namespace = "")]
    public List<Log> LogCollection { get; set; }
}
[XmlRoot("Log")]
[Serializable]
public class Log
{
    public Log() { }
    [XmlElement("InterfaceID")]
    public string InterfaceID { get; set; }
    [XmlElement("PrOrNumber")]
    public string PrOrNumber { get; set; }
    [XmlElement("MESKey")]
    public string MESKey { get; set; }
    [XmlElement("MsgStatusCode")]
    public string MsgStatusCode { get; set; }
    [XmlElement("MsgClass")]
    public string MsgClass { get; set; }
    [XmlElement("MsgNumber")]
    public string MsgNumber { get; set; }
    [XmlElement("MsgDescription")]
    public string MsgDescription { get; set; }
}

以及进行反序列化的函数

 InterfaceLog interfaceLogCollection = xmlString.Deserialize<InterfaceLog>();
    public static T Deserialize<T>(this string serializedObj) where T : class
    {
        XmlSerializer xs = new XmlSerializer(typeof(T));
        T result;
        using (TextReader reader = new StringReader(serializedObj))
            result = xs.Deserialize(reader) as T;
        return result;
    }

我尝试过的

  • 将类型更改为整数、十进制等
  • 将类型更改为字符串
  • 评论行是有效的,但这不是解决方案
  • 在Nillable上设置XSD标记

问题是我无法更改输入XML,因此添加xsi:nil="true"标记不是

输入字符串的格式不正确

的选项

我只能通过更改日志的类型来获得您所描述的行为。PrOrNumber属性设置为int或int?,Kilazur的第二个链接纠正了我复制中的问题——你确信你正确地遵循了它的建议吗?

在我的复制案例中,序列化程序在试图将一个空字符串解析为整数时遇到了麻烦。为了修复此问题,我们公开了一个字符串属性PrOrNumberRaw,供串行化器与之对话,其中包含所需的解析逻辑,以及我们自己的属性PrOrNumber,其中包含供应用程序使用的解析值(或null(。

Log类定义更改为:

[XmlRoot("Log")]
[Serializable]
public class Log
{
    public Log() { }
    [XmlElement("InterfaceID")]
    public string InterfaceID { get; set; }
    [XmlElement("MESKey")]
    public string MESKey { get; set; }
    [XmlElement("MsgStatusCode")]
    public string MsgStatusCode { get; set; }
    [XmlElement("MsgClass")]
    public string MsgClass { get; set; }
    [XmlElement("MsgNumber")]
    public string MsgNumber { get; set; }
    [XmlElement("MsgDescription")]
    public string MsgDescription { get; set; }
    [XmlElement("PrOrNumber")]
    public string PrOrNumberRaw
    {
        get { return this.PrOrNumber.ToString(); }
        set
        {
            if (!string.IsNullOrWhiteSpace(value))
            {
                this.PrOrNumber = int.Parse(value);
            }
            else
            {
                this.PrOrNumber = null;
            }
        }
    }
    [XmlIgnore]
    public int? PrOrNumber { get; set; }
}