如何连接windows中包含的XML数据.将XAML文件中的资源转换为XML.Linq
本文关键字:XML 文件 XAML 资源 转换 Linq 数据 何连接 连接 windows 包含 | 更新日期: 2023-09-27 18:11:43
我已经将表示NATO音标的XML数据放入XAML窗口文件中。我也有代码背后,我想以编程方式生成堆栈面板每个条目将包含2个标签,一个包含字母和第二个音标字表示它。它将全部放在一个包装板。我本可以少做很多,只是手工制作xaml,但这是练习,我真的想签出Linq和XML。后面的代码包含所有注释。这是我第一次在代码中使用Linq和XML,所以我真的不知道我在做什么,所以请解释这些概念并添加链接,而不仅仅是答案。
这里是XAML:<!--The XML database of letter of the phonetic alphabet this will be moved to seprate file-->
<Window.Resources>
<XmlDataProvider x:Key="PhoneticAlphabet" XPath="Alphabet/Entries" >
<x:XData>
<Alphabet xmlns="">
<Entries>
<Entry>
<Letter>Aa</Letter>
<Phonetic>Alpha</Phonetic>
</Entry>
<Entry>
<Letter>Bb</Letter>
<Phonetic>Bravo</Phonetic>
</Entry>
<Entry>
<Letter>Cc</Letter>
<Phonetic>Charlie</Phonetic>
</Entry>
<Entry>
<Letter>Dd</Letter>
<Phonetic>Delta</Phonetic>
</Entry>
<Entry>
<Letter>Ee</Letter>
<Phonetic>Echo</Phonetic>
</Entry>
<Entry>
<Letter>Ff</Letter>
<Phonetic>Foxtrot</Phonetic>
</Entry>
<Entry>
<Letter>Gg</Letter>
<Phonetic>Golf</Phonetic>
</Entry>
<Entry>
<Letter>Hh</Letter>
<Phonetic>Hotel</Phonetic>
</Entry>
<Entry>
<Letter>Ii</Letter>
<Phonetic>India</Phonetic>
</Entry>
<Entry>
<Letter>Jj</Letter>
<Phonetic>Juliett</Phonetic>
</Entry>
<Entry>
<Letter>Kk</Letter>
<Phonetic>Kilo</Phonetic>
</Entry>
<Entry>
<Letter>Ll</Letter>
<Phonetic>Lima</Phonetic>
</Entry>
<Entry>
<Letter>Mm</Letter>
<Phonetic>Mike</Phonetic>
</Entry>
<Entry>
<Letter>Nn</Letter>
<Phonetic>November</Phonetic>
</Entry>
<Entry>
<Letter>Oo</Letter>
<Phonetic>Oscar</Phonetic>
</Entry>
<Entry>
<Letter>Pp</Letter>
<Phonetic>Papa</Phonetic>
</Entry>
<Entry>
<Letter>Qq</Letter>
<Phonetic>Quebec</Phonetic>
</Entry>
<Entry>
<Letter>Rr</Letter>
<Phonetic>Romeo</Phonetic>
</Entry>
<Entry>
<Letter>Ss</Letter>
<Phonetic>Sierra</Phonetic>
</Entry>
<Entry>
<Letter>Tt</Letter>
<Phonetic>Tango</Phonetic>
</Entry>
<Entry>
<Letter>Uu</Letter>
<Phonetic>Uniform</Phonetic>
</Entry>
<Entry>
<Letter>Vv</Letter>
<Phonetic>Victor</Phonetic>
</Entry>
<Entry>
<Letter>Ww</Letter>
<Phonetic>Whiskey</Phonetic>
</Entry>
<Entry>
<Letter>Xx</Letter>
<Phonetic>Xray</Phonetic>
</Entry>
<Entry>
<Letter>Yy</Letter>
<Phonetic>Yankee</Phonetic>
</Entry>
<Entry>
<Letter>Zz</Letter>
<Phonetic>Zulu</Phonetic>
</Entry>
</Entries>
</Alphabet>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Window.DataContext>
<Binding Source="{StaticResource PhoneticAlphabet}"
XPath="*"/>
</Window.DataContext>
<Grid>
<WrapPanel x:Name="MainPanel">
</WrapPanel>
</Grid>
下面是后面的代码:
public partial class PhoneticAlphabetWindow : Window
{
public PhoneticAlphabetWindow()
{
InitializeComponent();
}
private void setControls() {
//Get XML data
//For ech xml Entry
//Make a stack Panel
//Make two labels with letter and phonetic and add them to stack panel
//Add a style to label
//add a content to be letter
//make a label with phonetic word
//addd a style to label
//add a phonetic
}
private IEnumerable<XElement> getData(String xml) {
XDocument doc = XDocument.Load(xml);
IEnumerable<XElement> childList =
from el in doc.Elements()
select el;
return childList;
}
private void addLabels(StackPanel EntryPanel, String letter, String phonetic) {
Label letterLabel = new Label();
letterLabel.Content = letter;
Label phoneticLabel = new Label();
phoneticLabel.Content = phonetic;
EntryPanel.Children.Add(letterLabel);
EntryPanel.Children.Add(phoneticLabel);
}
private StackPanel addStackPanel() {
StackPanel EntryPanel = new StackPanel();
EntryPanel.Orientation = Orientation.Horizontal;
MainPanel.Children.Add(EntryPanel);
return EntryPanel;
}
}
看一下这个链接,它应该很容易适应你的XAML XML代码:
https://www.intertech.com/Blog/query-an-xml-document-using-linq-to-xml/