WPF/C#学习笔记.2:Xml格式文件读取与通过XmlDataProvider以及资源模板“动态”绑定到TreeView

WPF/C#学习笔记.2

Xml格式文件读取与通过XmlDataProvider以及资源模板“动态”绑定到TreeView


What is XML?

XML一种树结构。
XML 文档必须包含且只能有一个根元素(RootNode)。该元素是所有其他元素的父元素。
XML 文档中的元素形成了一棵文档树。这棵树从根部开始,并扩展到树的最底端。所有元素均可拥有属性(Attribute)与子元素(ChildNode):


  
     a string
     
  

Tips of XML

  • 必须有标签的开始与关闭对 比如()和( ),而且“<”和“
  • Attribute的值用双引号围起来 比如aName="aValue"。

XML文件实例:



  
    Tomcat
    a supersonic, twin-engine, two-seat, variable-sweep wing fighter aircraft
    Grumman Aerospace Corporation
    38million
    
      2(Pilot and Radar Intercept Officer)
      62 ft 9 in (19.1 m)
      64 ft (19.55 m)
      38 ft (11.58 m)
       NACA 64A209.65 mod root, 64A208.91 mod tip
       43,735 lb (19,838 kg)
      61,000 lb (27,700 kg)
       74,350 lb (33,720 kg)
      2*General Electric F110-GE-400 afterburning turbofans
      16,610 lbf (73.9 kN) each
      30,200 lbf (134 kN) each
       16,200 lb internal; 20,000 lb with 2x 267 gallon external tanks
    
    
       Mach 2.34 (1,544 mph, 2,485 km/h) at high altitude
       500 nmi (575 mi, 926 km)
       1,600 nmi (1,840 mi, 2,960 km)
       50,000=""+ ft (15,200 m)
       LT45,000 ft/min (229 m/s)
       96 lb/ft2[164] (468.7 kg/m^2)
       0.88
     
  


Get more reference here

将XML通过XmlDataProvider,以及设置DataTemplate实现绑定到TreeView

MainWindow.xaml


    

        
        
            
            
                
            
            
                
                
                    
                
                
                
                    
                
            
        

        
        
            
                
                    
                        this is the 1st node
                        this is the 2rd node
                    
                
            
        

        
        
            
        
        
            
        
    

    
        
            
            
        
        
            

MainWindows.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Xml;

namespace xml2treeView
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        
        private void cmdExpandAll_Click(object sender, RoutedEventArgs e)
        {
            this.treeXml.Style = (Style)this.FindResource("treeView_AllExpanded");
        }

        private void cmdCollapseAll_Click(object sender, RoutedEventArgs e)
        {
            this.treeXml.Style = (Style)this.FindResource("treeView_AllCollapsed");
        }

        private void cmdLoadXml_Click(object sender, RoutedEventArgs e)
        {
            try {
                Microsoft.Win32.OpenFileDialog openFD = new Microsoft.Win32.OpenFileDialog();
                openFD.Filter = "XML Documents (*.xml)|*.xml|All Files (*.*)|*.*";
                Nullable isUserPickFile = openFD.ShowDialog(this);

                if(isUserPickFile == true) {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(openFD.FileName);
                    XmlDataProvider xmlDP = (XmlDataProvider)this.FindResource("xmlDataProvider");
                    xmlDP.Document = xmlDoc;
                    xmlDP.XPath = "*";
                }
            }
            catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

    } 
}


Try it

WPF/C#学习笔记.2:Xml格式文件读取与通过XmlDataProvider以及资源模板“动态”绑定到TreeView_第1张图片
初始效果
WPF/C#学习笔记.2:Xml格式文件读取与通过XmlDataProvider以及资源模板“动态”绑定到TreeView_第2张图片
loadXmlDocument

你可能感兴趣的:(WPF/C#学习笔记.2:Xml格式文件读取与通过XmlDataProvider以及资源模板“动态”绑定到TreeView)