如何在c#中读取XML文件并在xaml/c中生成动态按钮/扩展器#

如何在c#中读取XML文件并在xaml/c中生成动态按钮/扩展器#,c#,xml,wpf,C#,Xml,Wpf,我对WPF很陌生。 我得到了一个应该创建WPF应用程序的项目。我必须读取一个XML文件,并根据XML文件的内容创建一个带有扩展器或按钮的动态菜单 这是我的XML文件: <PROFIL_1009_L> <SAPNR VALUE="1009"/> <DESCR value="Name"/> <BAUTYP value="L"/> <FILE value="Filepath"/> </PROFIL_1009_L> <!--

我对WPF很陌生。 我得到了一个应该创建WPF应用程序的项目。我必须读取一个XML文件,并根据XML文件的内容创建一个带有扩展器或按钮的动态菜单

这是我的XML文件:

<PROFIL_1009_L>
<SAPNR VALUE="1009"/>
<DESCR value="Name"/>
<BAUTYP value="L"/>
<FILE value="Filepath"/>
</PROFIL_1009_L>
<!-- ======================================================= -->
<PROFIL_1009_M>
<SAPNR VALUE="1009"/>
<DESCR value="Name">
<BAUTYP value="M"/>
<FILE value="Filepath"/>
<TECHNPLATZ_01>
<TPLNR value="0001"/>
<STRNO value="SAPLineName01"/>
<PLTXT value="LineName1"/>
</TECHNPLATZ_01>
<TECHNPLATZ_02>
<TPLNR value="0002"/>
<STRNO value="SAPLineName02"/>
<PLTXT value="LineName2"/>
</TECHNPLATZ_02>
<TECHNPLATZ_03>
<TPLNR value="003"/>
<STRNO value="SAPLineName03"/>
<PLTXT value="LineName3"/>
</TECHNPLATZ_03>
</PROFIL_1009_M>
<!-- ======================================================= -->
<PROFIL_1009_V>
<SAPNR VALUE="1009"/>
<DESCR value="Name>
<BAUTYP value="V"/>
<FILE value="Filepath"/>
</PROFIL_1009_V>


使用这样的树视图

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const string FILENAME = @"c:\temp\test.xml";
        public Form1()
        {
            InitializeComponent();

            XElement bautyp = null;
            XElement file = null;
            XDocument doc = XDocument.Load(FILENAME);

            List<XElement> profils = doc.Descendants().Where(x => x.Name.LocalName.StartsWith("PROFIL_1009")).ToList();

            foreach (XElement profil in profils)
            {
                TreeNode newNode =  treeView1.Nodes.Add(profil.Name.LocalName);

                XElement sapnr = profil.Element("SAPNR");
                if(sapnr != null)
                {
                    newNode.Nodes.Add(string.Join(" : ",new string[] {"SAPNR", (string)sapnr.Attribute("VALUE")}));
                }
                XElement descr = profil.Element("DESCR");
                if (descr != null)
                {
                    TreeNode descrNode = newNode.Nodes.Add(string.Join(" : ", new string[] { "DESCR", (string)descr.Attribute("value") }));

                    bautyp = descr.Element("BAUTYP");
                    if (bautyp != null)
                    {
                        descrNode.Nodes.Add(string.Join(" : ", new string[] { "BAUTYP", (string)bautyp.Attribute("value") }));
                    }
                    file = descr.Element("FILE");
                    if (file != null)
                    {
                        descrNode.Nodes.Add(string.Join(" : ", new string[] { "FILE", (string)file.Attribute("value") }));
                    }
                    List<XElement> technplatz = descr.Elements().Where(x => x.Name.LocalName.StartsWith("TECHNPLATZ")).ToList();
                    foreach (XElement technplat in technplatz)
                    {
                        TreeNode tplnrNode = descrNode.Nodes.Add(technplat.Name.LocalName);
                        XElement tplnr = technplat.Element("TPLNR");
                        if (tplnr != null)
                        {
                            tplnrNode.Nodes.Add(string.Join(" : ", new string[] { "TPLNR", (string)tplnr.Attribute("value") }));
                        }
                        XElement strno = technplat.Element("STRNO");
                        if (strno != null)
                        {
                            tplnrNode.Nodes.Add(string.Join(" : ", new string[] { "STRNO", (string)strno.Attribute("value") }));
                        }
                        XElement pltxt = descr.Element("PLTXT");
                        if (pltxt != null)
                        {
                            tplnrNode.Nodes.Add(string.Join(" : ", new string[] { "PLTXT", (string)pltxt.Attribute("value") }));
                        }
                    }
                }
                bautyp = profil.Element("BAUTYP");
                if (bautyp != null)
                {
                    newNode.Nodes.Add(string.Join(" : ", new string[] { "BAUTYP", (string)bautyp.Attribute("value") }));
                }
                file = profil.Element("FILE");
                if (file != null)
                {
                    newNode.Nodes.Add(string.Join(" : ", new string[] { "FILE", (string)file.Attribute("value") }));
                }


            }
            treeView1.ExpandAll();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Xml;
使用System.Xml.Linq;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
常量字符串文件名=@“c:\temp\test.xml”;
公共表格1()
{
初始化组件();
XElement bautyp=null;
XElement file=null;
XDocument doc=XDocument.Load(文件名);
列出profils=doc.substands()。其中(x=>x.Name.LocalName.StartsWith(“PROFIL_1009”)).ToList();
foreach(profils中的XElement profil)
{
TreeNode newNode=treeView1.Nodes.Add(profil.Name.LocalName);
XElement sapnr=轮廓元素(“sapnr”);
如果(sapnr!=null)
{
添加(string.Join(“:”,新字符串[]{“SAPNR”,(string)SAPNR.Attribute(“VALUE”)});
}
XElement descr=轮廓元素(“descr”);
如果(描述!=null)
{
TreeNode descrNode=newNode.Nodes.Add(string.Join(“:”,新字符串[]{“DESCR”,(string)DESCR.Attribute(“value”)});
bautyp=描述元素(“bautyp”);
如果(bautyp!=null)
{
添加(string.Join(“:”,新字符串[]{“BAUTYP”,(string)BAUTYP.Attribute(“value”)});
}
file=descr.Element(“文件”);
如果(文件!=null)
{
添加(string.Join(“:”,新字符串[]{“FILE”,(string)FILE.Attribute(“value”)});
}
列出technplatz=descr.Elements()。其中(x=>x.Name.LocalName.StartsWith(“technplatz”)).ToList();
foreach(technplatz中的XElement technplat)
{
TreeNode tplnrNode=descrNode.Nodes.Add(technplat.Name.LocalName);
XElement tplnr=technplat.Element(“tplnr”);
如果(tplnr!=null)
{
添加(string.Join(“:”,新字符串[]{“TPLNR”,(string)TPLNR.Attribute(“value”)});
}
XElement strno=technplat.Element(“strno”);
如果(strno!=null)
{
添加(string.Join(“:”,新字符串[]{“STRNO”,(string)STRNO.Attribute(“value”)});
}
XElement pltxt=描述元素(“pltxt”);
如果(pltxt!=null)
{
添加(string.Join(“:”,新字符串[]{“PLTXT”,(string)PLTXT.Attribute(“value”)});
}
}
}
bautyp=轮廓元素(“bautyp”);
如果(bautyp!=null)
{
添加(string.Join(“:”,新字符串[]{“BAUTYP”,(string)BAUTYP.Attribute(“value”)});
}
文件=profil.Element(“文件”);
如果(文件!=null)
{
添加(string.Join(“:”,新字符串[]{“FILE”,(string)FILE.Attribute(“value”)});
}
}
treeView1.ExpandAll();
}
}
}

谢谢,您为我提供了解决问题所需的正确输入!。