如何使用LINQ to XML将XML数据绑定到用户控件 [英] How do I bind XML data to a user control with LINQ to XML
问题描述
我正在构建一个UserContorl库。 XAML由一个组合框和几个文本框组成。 < note:i =have =this =code =working =with =but =wanted =to =streamline =it =linq = xml =xmlns:note =#unknown>
我有一个外部XML文档,其中包含客户,地址,电话号码,联系人列表名称和电子邮件地址。
我想导入XML文档以填充名为cbxName的ComboBox,其中包含每个名称字段的内容在XML中。 当在下拉列表中更改选择时,我希望其他文本框绑定到相同的数据并反映更改。
我还会有一个按钮来保存对表单中数据所做的任何更改。
XML结构:
I have a UserContorl library that I am building. The XAML consists of a combobox and several textboxes. <note: i="" have="" this="" code="" working="" with="" but="" wanted="" to="" streamline="" it="" linq="" xml="" xmlns:note="#unknown">
I have an external XML document with a list of customers, addresses, phone numbers, contact names, and email addresses.
I want to import the XML document to populate the ComboBox called cbxName, with the contents of each Name field in the XML. When the selection is changed in the dropdown, I want the other textboxes bound to the same data and reflect the changes.
I will also have a button to save any changes made to the data in the form.
XML Structure:
<?xml version="1.0" encoding="utf-8"?>
<CustDB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Customer>
<Name></Name>
<Address1></Address1>
<Address2></Address2>
<Phone></Phone>
<Fax></Fax>
<Cell></Cell>
<Other></Other>
<Attn></Attn>
<Email></Email>
<SalesPerson></SalesPerson>
</Customer>
</CustDB>
我现在的VB看起来像这样......
My current VB looks like this...
Imports c = Corel.Interop.VGCore
Imports System
Imports System.Xml
Imports System.Linq
Public Class Docker
Dim WithEvents appDRAW As c.Application
Public Sub New(app As Object)
InitializeComponent()
appDRAW = CType(app, c.Application)
Dim contacts As XDocument = XDocument.Load("c:\users\sean\documents\custdbTesting.xml")
End Sub
End Class
我的XAML看起来像这样...
My XAML looks like this...
<UserControl x:Class="Docker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignWidth="250" FontSize="10">
<UserControl.Resources>
<Style x:Key="labelStyle" TargetType="{x:Type Label}">
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
<Style x:Key="textboxStyle" TargetType="{x:Type TextBox}">
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="Height" Value="23"/>
<Setter Property="Width" Value="175"/>
</Style>
<Style x:Key="checkboxStyle" TargetType="{x:Type CheckBox}">
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
<Style x:Key="comboboxStyle" TargetType="{x:Type ComboBox}">
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="Height" Value="23"/>
<Setter Property="Width" Value="175"/>
<Setter Property="IsEditable" Value="True"/>
</Style>
</UserControl.Resources>
<StackPanel>
<Grid Margin="5">
<Label Style="{StaticResource labelStyle}" Content="Name"/>
<ComboBox x:Name="cbxName" Style="{StaticResource comboboxStyle}"/>
<Label Style="{StaticResource labelStyle}" Content="Address" Margin="0,23,0,0"/>
<TextBox x:Name="txbAdd1" Style="{StaticResource textboxStyle}" Margin="0,23,0,0" TextChanged="TextBox_TextChanged"/>
<TextBox x:Name="txbAdd2" Style="{StaticResource textboxStyle}" Margin="0,46,0,0"/>
<Label Style="{StaticResource labelStyle}" Content="Phone" Margin="0,69,0,0"/>
<TextBox x:Name="txbPhone" Style="{StaticResource textboxStyle}" Margin="0,69,0,0"/>
<Label Style="{StaticResource labelStyle}" Content="Fax" Margin="0,92,0,0"/>
<TextBox x:Name="txbFax" Style="{StaticResource textboxStyle}" Margin="0,92,0,0"/>
<Label Style="{StaticResource labelStyle}" Content="Cell" Margin="0,115,0,0"/>
<TextBox x:Name="txbCell" Style="{StaticResource textboxStyle}" Margin="0,115,0,0"/>
<Label Style="{StaticResource labelStyle}" Content="Other" Margin="0,138,0,0"/>
<TextBox x:Name="txbOther" Style="{StaticResource textboxStyle}" Margin="0,138,0,0"/>
<Label Style="{StaticResource labelStyle}" Content="Attn" Margin="0,161,0,0"/>
<TextBox x:Name="txbAttn" Style="{StaticResource textboxStyle}" Margin="0,161,0,0"/>
<Label Style="{StaticResource labelStyle}" Content="Email" Margin="0,184,0,0"/>
<TextBox x:Name="txbEmail" Style="{StaticResource textboxStyle}" Margin="0,184,0,0"/>
<Label Style="{StaticResource labelStyle}" Content="Sales Rep" Margin="0,207,0,0"/>
<ComboBox x:Name="cbxSales" Style="{StaticResource comboboxStyle}" Margin="0,207,0,0"/>
<Button Content="Save Data" Height="23" Width="100" Margin="70,235,70,0"/>
</Grid>
</StackPanel>
</UserControl>
推荐答案
我不确定你有什么问题。然而......
看看例子:
I'm not sure what kind of issue do you have. Nevertheless...
Have a look at example:
XElement contacts =
new XElement("Contacts",
new XElement("Contact",
new XElement("Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144",
new XAttribute("Type", "Home")),
new XElement("Phone", "425-555-0145",
new XAttribute("Type", "Work")),
new XElement("Address",
new XElement("Street1", "123 Main St"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042"))
),
new XElement("Contact",
new XElement("Name", "Anna Hives"),
new XElement("Phone", "206-556-0145",
new XAttribute("Type", "Home")),
new XElement("Phone", "425-556-0146",
new XAttribute("Type", "Work")),
new XElement("Address",
new XElement("Street1", "124 Main St"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042"))
)
);
var qry = from c in contacts.Elements("Contact").Elements("Name")
select c.Value;
foreach(string n in qry)
{
Console.WriteLine("Name='{0}'", n);
}
结果:
Result:
Name='Patrick Hines'
Name='Anna Hives'
欲了解更多信息:带示例的LINQ to XML教程 [ ^ ]
这是 VB.NET 实施:
For further information: LINQ To XML Tutorials with Examples[^]
Here is VB.NET implementation:
Dim sFileName As String = "D:\custdbTesting.xml"
Dim contacts As XDocument = XDocument.Load(sFileName)
Dim qry As IEnumerable = From c In contacts.Elements("CustDB").Elements("Customer").Elements("Name") _
Order By c.Value Ascending
Select c.Value
For Each n in qry
Console.WriteLine("Name='{0}'", n)
Next
结果:
Result:
Name='A'
...
Name='Z'
[/ EDIT]
[/EDIT]
这篇关于如何使用LINQ to XML将XML数据绑定到用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!