我创建了这样的类:
[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。我哪里弄错了?如何解决?
答案 0 :(得分: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; }
}