2016-05-31 31 views
0

我建立了一個這樣的類:反序列化的XML對象,錯誤的價值觀

[XmlRoot(ElementName = "Control", Namespace = "http://schemas.microsoft.com/VisualStudio/2011/storyboarding/stencilDocument"),Serializable] 
    public class ControlStencilXML 
    { 
     [XmlAttribute("ReferenceName")] 
     public string ReferenceName; 
     [XmlAttribute("DisplayName")] 
     public string DisplayName; 
     [XmlAttribute("Revision")] 
     public int Revision; 
     [XmlAttribute("IsBackground")] 
     public bool IsBackground; 
     [XmlAttribute("DefaultInsertionPoint")] 
     public string DefaultInsertionPoint; 
     [XmlAttribute("IsAnimated")] 
     public bool IsAnimated; 
     [XmlAttribute("KeyWords")] 
     public string KeyWords; 
     [XmlAttribute("ToolTip")] 
     public string ToolTip; 
     [XmlElement("ShapeLayouts")] 
     public List<ShapeLayout> ShapeLayouts; 
    } 

[XmlRoot(ElementName = "ShapeLayout", Namespace = "http://schemas.microsoft.com/VisualStudio/2011/storyboarding/stencilDocument")] 
public class ShapeLayout 
{ 
    [XmlAttribute("Name")] 
    string Name; 
    [XmlAttribute("HorizontalAlignment")] 
    string HorizontalAlignment; 
    [XmlAttribute("VerticalAlignment")] 
    string VerticalAlignment; 
} 

XLM,我想反序列化:

<?xml version="1.0" encoding="utf-8"?> 
<Control ReferenceName="System.Storyboarding.Common.CheckBoxChecked" DisplayName="Checkbox (checked)" ToolTip="Checked checkbox with a text label" Revision="1" IsBackground="false" DefaultInsertionPoint="Default" Keywords="Toggle, Phone" IsAnimated="false" xmlns="http://schemas.microsoft.com/VisualStudio/2011/storyboarding/stencilDocument"> 
    <ShapeLayouts> 
    <ShapeLayout Name="Check" HorizontalAlignment="Left" VerticalAlignment="Center" /> 
    <ShapeLayout Name="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Center" /> 
    <ShapeLayout Name="Content" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> 
    </ShapeLayouts> 
</Control> 

沒有錯誤,當我試圖反序列化的問題是我只獲取第一個ShapeLayout對象,並且它的所有值都等於null。我在哪裏犯錯?如何解決它?

回答

1

你需要一個更包裝對象,以符合規定XML -

[XmlRoot(ElementName = "ShapeLayouts", Namespace = "http://schemas.microsoft.com/VisualStudio/2011/storyboarding/stencilDocument")] 
public class ShapeLayouts 
{ 
    [XmlElement(ElementName = "ShapeLayout", Namespace = "http://schemas.microsoft.com/VisualStudio/2011/storyboarding/stencilDocument")] 
    public List<ShapeLayout> ShapeLayout { get; set; } 
} 
+0

非常感謝你,工作。我也將ShapeLayout字段的可見性更改爲public。 – buks