来自XML的WPF TreeView(动态)

时间:2014-03-11 23:02:30

标签: c# xml wpf treeview

我想从可用的XML数据构建WPF中的Hierarchical TreeView。这个数据可能是动态的,因此Tree可能最终会有很多级别的子节点。我的示例XML如下所示:

    <Groups>
      <Group Name="Global" ID="1">
        <Group Name="Demo - Marketing" ID="2" />
        <Group Name="Demo - Finance" ID="3" />
        <Group Name="Demo - Operations" ID="4" />
        <Group Name="Demo - Sandpit" ID="5" />
        <Group Name="Morson" ID="6">
          <Group Name="Internal" ID="29">
            <Group Name="Branch" ID="31">
              <Group Name="Branch Components WIP" ID="50" />
              <Group Name="MI32_IT Recruitment" ID="51" />
              <Group Name="Branch Master" ID="52" />
                  <Group Name="Branch Master_Perm" ID="76" />
                  <Group Name="MI42_Manchester Rail Technical" ID="79" />
                  <Group Name="MI39_London Technical" ID="86" />
                    <Group Name="MI51_Oil &amp; Gas London" ID="87" />
              <Group Name="MI40_London Support Services" ID="88" />
                  <Group Name="MI28_Manchester Rail" ID="119" />
                  <Group Name="MI35_Heathrow" ID="125" />
              <Group Name="MI73_London Telco" ID="150" />
              <Group Name="MI04_EPB" ID="158" />          
            </Group>
            <Group Name="Consultant" ID="32">
              <Group Name="Consultant Master " ID="53" />
              <Group Name="MI32_SBS_Steven Byrne" ID="57" />
              <Group Name="MI32_RH_Robert Hoffman" ID="58" />
                <Group Name="MI32_J3D_Jonathan Darbyshire" ID="60" />
                    <Group Name="MI32_BS_Bobby Sethi" ID="61" />
                        <Group Name="MI32_DM_Diana Mathers" ID="62" />
                            <Group Name="MI32_KER_Kerry Redmond" ID="63" />
              <Group Name="MI32_MO_Marc Oldland" ID="65" />
            </Group>
            <Group Name="Director" ID="33">
              <Group Name="Director_Client Internal" ID="320" />
              <Group Name="Director_Client External" ID="321" />
              <Group Name="Director_Net Starters_Finishers" ID="400" />
              <Group Name="Director AWR" ID="455" />
              <Group Name="Director Board Data_Gareth Owen" ID="488" />
            </Group>
            <Group Name="Finance" ID="36">
              <Group Name="Fin_Net Starters_Finishers" ID="401" />
              <Group Name="Fin_Turnover (Unadjusted)" ID="464" />
              <Group Name="Fin_Systems Spend" ID="487" />
              <Group Name="Fin_Sales Ledger" ID="504" />
            </Group>
            <Group Name="System Administration" ID="44" />
            <Group Name="Account Manager" ID="68">
              <Group Name="Account Manager Master" ID="69" />
              <Group Name="URS" ID="70" />
              <Group Name="Chubb" ID="71" />
              <Group Name="Welsh Water" ID="72" />
              <Group Name="Thales" ID="74" />
              <Group Name="Costain" ID="75" />
              <Group Name="Airbus" ID="124" />
            </Group>
            <Group Name="Branch Administration" ID="330">
              <Group Name="MI39_Admin_London Technical" ID="331" />
              <Group Name="Branch Administration Master" ID="351" />
              <Group Name="MI19_Admin_Science" ID="363" />
                  <Group Name="MI51_Admin_Oil &amp; Gas London" ID="364" />
                  <Group Name="MI02_Admin_Aerospace_Preston" ID="367" />
              <Group Name="MI02_Admin_Aerospace_South" ID="471" />
              <Group Name="MI02_Admin_Aerospace_South_Nicola Leggett" ID="485" />
              <Group Name="MO01_Admin_Houston" ID="495" />
            </Group>
            <Group Name="Payroll" ID="414" />
            <Group Name="Business Development" ID="454" />
            <Group Name="JC - Work In Progress (JDV)" ID="503" />
            <Group Name="JC - Work In Progress (Vencuro)" ID="507" />
            <Group Name="Sales Ledger View - Work in Progress" ID="512" />
            <Group Name="JC - Work In Progress (Thales)" ID="543" />
          </Group>
          <Group Name="External" ID="30">
            <Group Name="Client" ID="34">
              <Group Name="Client Master" ID="54" />
              <Group Name="Costain" ID="73" />
            </Group>
          </Group>
          <Group Name="Demo Dashboards" ID="47" />
          <Group Name="Master Components" ID="416" />
          <Group Name="Demo Designer" ID="531" />
        </Group>
      </Group>
    </Groups>

如何以分层方式使用组名实现树视图? 示例XAML和要绑定项目的类 会非常有用。 - 编程语言C#

非常感谢提前。 -Vinnie

1 个答案:

答案 0 :(得分:0)

首先,您需要从XML模式创建类,如下所示:

[XmlRoot("Groups")]
public class GroupsXml
{
    [XmlElement("Group", typeof(Group))]
    public List<Group> GroupsList { get; set; }
}

public class Group
{
    [XmlAttribute("ID")]
    public int Id { get; set; }

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

然后,您需要将xml字符串序列化为您的案例中的对象或组列表,例如:

public List<Group> ReadGroupsFromXmlFile(string fileName)
{
    var groupsXml = new GroupsXml();
    var groupsXmlFile = xDocument.Load(fileName).ToString();
    var groupType = groupsXml .GetType();
    var oXmlSerializer = new XmlSerializer(groupType);
    groupsXml = (GroupsXml)oXmlSerializer.Deserialize(new StringReader(groupsXmlFile ));
            return categoriesXml.GroupsList;
}

我希望它能回答你的问题