XML反序列化卡住

时间:2018-03-22 07:55:14

标签: c# xml xsd

我使用priceResponse.cs从xml生成xsd文件,然后从xsd文件生成xsd.exe

以下是priceResponse.cs代码:

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.6.1055.0.
// 


/// <uwagi/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class document {

    private object[] itemsField;

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute("DATASETS", typeof(documentDATASETS), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("ERROR", typeof(documentERROR), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("PRICE", typeof(documentPRICE), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

/// <uwagi/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class documentDATASETS {

    private string cOUNTRYField;

    private string cURRENCYField;

    private string pOSTCODEMASKField;

    private string tOWNGROUPField;

    private string sERVICEField;

    private string oPTIONField;

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string COUNTRY {
        get {
            return this.cOUNTRYField;
        }
        set {
            this.cOUNTRYField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string CURRENCY {
        get {
            return this.cURRENCYField;
        }
        set {
            this.cURRENCYField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string POSTCODEMASK {
        get {
            return this.pOSTCODEMASKField;
        }
        set {
            this.pOSTCODEMASKField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string TOWNGROUP {
        get {
            return this.tOWNGROUPField;
        }
        set {
            this.tOWNGROUPField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string SERVICE {
        get {
            return this.sERVICEField;
        }
        set {
            this.sERVICEField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string OPTION {
        get {
            return this.oPTIONField;
        }
        set {
            this.oPTIONField = value;
        }
    }
}

/// <uwagi/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class documentERROR {

    private string cODEField;

    private string dESCRIPTIONField;

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string CODE {
        get {
            return this.cODEField;
        }
        set {
            this.cODEField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string DESCRIPTION {
        get {
            return this.dESCRIPTIONField;
        }
        set {
            this.dESCRIPTIONField = value;
        }
    }
}

/// <uwagi/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class documentPRICE {

    private string rATEIDField;

    private string sERVICEField;

    private string sERVICEDESCField;

    private string oPTIONField;

    private string oPTIONDESCField;

    private string cURRENCYField;

    private string rATEField;

    private string rESULTField;

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string RATEID {
        get {
            return this.rATEIDField;
        }
        set {
            this.rATEIDField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string SERVICE {
        get {
            return this.sERVICEField;
        }
        set {
            this.sERVICEField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string SERVICEDESC {
        get {
            return this.sERVICEDESCField;
        }
        set {
            this.sERVICEDESCField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string OPTION {
        get {
            return this.oPTIONField;
        }
        set {
            this.oPTIONField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string OPTIONDESC {
        get {
            return this.oPTIONDESCField;
        }
        set {
            this.oPTIONDESCField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string CURRENCY {
        get {
            return this.cURRENCYField;
        }
        set {
            this.cURRENCYField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string RATE {
        get {
            return this.rATEField;
        }
        set {
            this.rATEField = value;
        }
    }

    /// <uwagi/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string RESULT {
        get {
            return this.rESULTField;
        }
        set {
            this.rESULTField = value;
        }
    }
}

我正在尝试反序列化priceResponse.xml,但似乎它被卡住了。我认为名称空间存在问题,而且我正在进行反序列化错误。

这是xml代码:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document>
    <DATASETS>
        <COUNTRY>UTD</COUNTRY>
        <CURRENCY>UTD</CURRENCY>
        <POSTCODEMASK>UTD</POSTCODEMASK>
        <TOWNGROUP>UTD</TOWNGROUP>
        <SERVICE>UTD</SERVICE>
        <OPTION>UTD</OPTION>
    </DATASETS>
    <ERROR>
        <CODE>P13</CODE>
        <DESCRIPTION>RATEID: 1 - Standard Rates</DESCRIPTION>
    </ERROR>
    <PRICE>
        <RATEID>1</RATEID>
        <SERVICE>09N</SERVICE>
        <SERVICEDESC>9:00 Express</SERVICEDESC>
        <OPTION>NONE</OPTION>
        <OPTIONDESC>NONE</OPTIONDESC>
        <CURRENCY>PLN</CURRENCY>
        <RATE>712.93</RATE>
        <RESULT>Y</RESULT>
    </PRICE>
    <PRICE>
        <RATEID>1</RATEID>
        <SERVICE>10N</SERVICE>
        <SERVICEDESC>10:00 Express</SERVICEDESC>
        <OPTION>NONE</OPTION>
        <OPTIONDESC>NONE</OPTIONDESC>
        <CURRENCY>PLN</CURRENCY>
        <RATE>706.14</RATE>
        <RESULT>Y</RESULT>
    </PRICE>
    <PRICE>
        <RATEID>1</RATEID>
        <SERVICE>12N</SERVICE>
        <SERVICEDESC>12:00 Express</SERVICEDESC>
        <OPTION>NONE</OPTION>
        <OPTIONDESC>NONE</OPTIONDESC>
        <CURRENCY>PLN</CURRENCY>
        <RATE>689.84</RATE>
        <RESULT>Y</RESULT>
    </PRICE>
    <PRICE>
        <RATEID>1</RATEID>
        <SERVICE>15N</SERVICE>
        <SERVICEDESC>Express</SERVICEDESC>
        <OPTION>NONE</OPTION>
        <OPTIONDESC>NONE</OPTIONDESC>
        <CURRENCY>PLN</CURRENCY>
        <RATE>670.03</RATE>
        <RESULT>Y</RESULT>
    </PRICE>
</document>

最后反序列化代码:

File.WriteAllText("priceResponse.xml", x);
//Console.WriteLine(x);

var ser = new XmlSerializer(typeof(document),new XmlRootAttribute("documentPRICE"));
using (var reader = XmlReader.Create("priceResponse.xml"))
{
    var wrapper = (document)ser.Deserialize(reader);

    foreach (documentPRICE item in wrapper.Items)
    {
        Console.WriteLine(item.OPTIONDESC);
    }
}

我想在PRICE元素中获取每个项目,但我无法反序列化xml。我究竟做错了什么 ?

1 个答案:

答案 0 :(得分:0)

在这行代码中,

var ser = new XmlSerializer(typeof(document),new XmlRootAttribute("documentPRICE"));

您已经序列化了文档对象,但是将xml的根保持为documentPRICE。在这里,

var wrapper = (document)ser.Deserialize(reader);

您正在尝试将其反序列化为document对象,这是没有意义的。 在您的案例中序列化后创建的xml具有documentPRICE作为根对象,但您尝试将相同的序列反序列化为document对象,这是错误的。您需要做的是在序列化时将document作为根目录,如下所示。

var ser = new XmlSerializer(typeof(document), new XmlRootAttribute("document"));

它应该有用。