2016-05-31 48 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