部分xml消息未正确反序列化

时间:2012-08-23 05:49:15

标签: c# .net xml serialization xml-serialization

您好我有以下反序列化代码

        public static T DeserializeXML<T>(String xml) where T : class
        {
            T newObject = null;
            XmlSerializer s = new XmlSerializer(typeof(T));
            using (StringReader sw = new StringReader(xml))
            {
                newObject = (T)s.Deserialize(sw);
            }
            return newObject;
        }

我尝试反序列化的消息

<Data>
<ItemIn date="2012-08-09T10:25:54.06+01:00" itemId="000007721" Id="1">   <Extensions><Info Id="parts" order="issue"/></Extensions></ItemIn>
</Data>

但是我从来没有让Extensions部分回到最初的课程我总是得到null。其余的课程还可以。

有什么建议要检查吗?

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ItemTransferIn {

    private Extensions extensions;

    private System.DateTime date;

    private string itemId;

    private string Id;

    /// <remarks/>
    public ItemTransferInExtensions Extensions {
        get {
            return this.extensions;
        }
        set {
            this.extensions = value;
        }
    }

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class Extensions {

    private RecipeInfo recipeInfoField;

    /// <remarks/>
    public RecipeInfo RecipeInfo {
        get {
            return this.recipeInfoField;
        }
        set {
            this.recipeInfoField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class RecipeInfo {

    private string recipeIdField;

    private string orderIdField;

    private string itemBarcodeIdField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Id {
        get {
            return this.recipeIdField;
        }
        set {
            this.recipeIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string order {
        get {
            return this.orderIdField;
        }
        set {
            this.orderIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string itemBarcodeId {
        get {
            return this.itemBarcodeIdField;
        }
        set {
            this.itemBarcodeIdField = value;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

对于扩展......看起来你已经编辑了代码;有

private Extensions extensions;

...

/// <remarks/>
public ItemTransferInExtensions Extensions {
    get {
        return this.extensions;
    }
    set {
        this.extensions = value;
    }
}

坦率地说,这甚至不应该编译;我们没有 ItemTransferInExtensions类。

此外,Info无效:

/// <remarks/>
public RecipeInfo RecipeInfo {...blah...}

<Info ..../>不符。所以要么纠正xsd并重新生成cs,要么纠正xml;但目前他们不匹配

RecipeInfo属性重命名为Info(您也可以添加属性)并修复Extensions / ItemTransferInExtensions(以及缺少的}),并添加根类匹配xml:

public class Data
{
    public ItemTransferIn ItemIn { get; set; }
}

......一切正常:

static void Main()
{
    string msg = @"<Data>
<ItemIn date=""2012-08-09T10:25:54.06+01:00"" itemId=""000007721"" Id=""1"">   <Extensions><Info Id=""parts"" order=""issue""/></Extensions></ItemIn>
</Data>";
    var obj = DeserializeXML<Data>(msg);

    Console.WriteLine(obj.ItemIn.Extensions.Info.order); // issue
}

坦率地说,手动操作更容易:

public class Data
{
    public ItemTransferIn ItemIn { get; set; }
}
public class ItemTransferIn
{
    [XmlAttribute("date")]
    public DateTime Date { get; set; }

    [XmlAttribute("itemId")]
    public string Itemid { get; set; }

    [XmlAttribute]
    public int Id { get; set; }

    public Extensions Extensions { get; set; }
}
public class Extensions
{
    public ExtensionsInfo Info { get; set; }
}
public class ExtensionsInfo
{
    public int Id { get; set; }
    [XmlAttribute("order")]
    public string Order { get; set; }
}