簡體   English   中英

輸入字符串的格式不正確

[英]Input string was not of the correct format

我正在嘗試反序列化和驗證為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-or-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"標記不是一個選項

我只能通過將Log.PrOrNumber屬性的類型更改為int或int?來獲取您描述的行為,而Kilazur的第二個鏈接更正了我的復制中的問題 - 您確定您正確地遵循了它的建議嗎?

在我的再現案例中,序列化器試圖將空字符串解析為整數。 為了解決這個問題,我們為串行PrOrNumberRaw提供了一個字符串屬性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; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM