1

I have XML-based config for application shortcuts bindings. i need to parse it.

<ShortcutBinding>
  <ShortcutHandler Name ="Retail.Application.Documents.Outcome.Presentation.OutcomePresenter">
    <Shortcut Name="EditHeader">
      <Key>CTRL</Key>
      <Key>F4</Key>
    </Shortcut>
    <Shortcut Name="EditItem">
      <Key>F4</Key>
    </Shortcut>
  </ShortcutHandler>  
</ShortcutBinding>

I know that .Net has attributes for deserializing XML into objects. Can anyone write complete example for such deserialization, using attributes.

4

1 回答 1

1
public class ShortcutBinding
{
    public ShortcutHandler ShortcutHandler { get; set; }
}

public class ShortcutHandler
{
    [XmlAttribute]
    public string Name { get; set; }

    [XmlElement("Shortcut")]
    public List<Shortcut> Shortcuts { get; set; }
}

public class Shortcut
{
    [XmlAttribute]
    public string Name { get; set; }

    [XmlElement("Key")]
    public List<string> Keys { get; set; }
}

反序列化:

XmlSerializer serializer = new XmlSerializer(typeof(ShortcutBinding));
var binding = (ShortcutBinding)serializer.Deserialize(XmlReader.Create(path));
于 2013-06-26T13:37:38.877 回答