我仍在学习WPF,C#和xml文件。 我有具有现有内容的xml文件。 我想做的是:
我想显示类似这样的内容(仅示例,不是真实数据):
文件名= Eqpt ID
文件位置=装备操作
名称=二进制代码
姓氏= bin描述,
电话号码=垃圾桶质量
以此类推。
但是问题是,当我运行应用程序时,它像这样出来 这是我尝试过的:
string sampleXMLFile = FilePath.XmlBinCode;
DataSet dataSet = new DataSet();
dataSet.ReadXml(sampleXMLFile);
DataView dataView = new DataView(dataSet.Tables[0]);
dataGrid_ConfigurableRejectBinCode.ItemsSource = dataView;
这是我的xml文件的预览:
这是我的XAML的预览:
<DockPanel Background="White">
<Button Content="Load File" Click="LoadXmlFile_Click" DockPanel.Dock="Top"/>
<DataGrid x:Name="dataGrid_ConfigurableRejectBinCode"
DockPanel.Dock="Bottom"
CanUserAddRows="True"
CanUserDeleteRows="True"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False">
<DataGrid.Columns>
<!--<DataGridTextColumn Header="Bin Code" Binding="{Binding BinCode}"/>
<DataGridTextColumn Header="Bin Description" Binding="{Binding Path=Element[BinDescription].Value}"/>
<DataGridTextColumn Header="Bin Quality" Binding="{Binding Path=Element[BinQuality].Value}"/>
<DataGridTextColumn Header="Pick" Binding="{Binding Path=Element[Pick].Value}"/>
<DataGridTextColumn Header="Vision Station" Binding="{Binding Path=Element[VisionStation].Value}"/>
<DataGridTextColumn Header="Vision IO" Binding="{Binding Path=Element[VisionIO].Value}"/>-->
</DataGrid.Columns>
</DataGrid>
</DockPanel>
如何像我上面想要的那样显示xml数据?请原谅我的语言,因为英语不是我的母语。 请帮助我,谢谢你
答案 0 :(得分:0)
我已经找到了显示它的方法,这就是我的方法
make类并绑定所有要在XAML控件中显示的细节。 这是XAML
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Eqpt ID"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Equip Opn"/>
<TextBox Grid.Row="0" Grid.Column="1" IsReadOnly="True" Text="{Binding EqptID}"/>
<TextBox Grid.Row="1" Grid.Column="1" IsReadOnly="True" Text="{Binding EquipOpn}"/>
<DataGrid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" x:Name="DataGrid_BinDefinition"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False"
HorizontalContentAlignment="Center"
ItemsSource="{Binding BinDefinition}">
<DataGrid.Columns>
<DataGridTextColumn Header="Bin Code" Binding="{Binding BinCode}"/>
<DataGridTextColumn Header="Bin Description" Binding="{Binding BinDescription}"/>
<DataGridTextColumn Header="Bin Quality" Binding="{Binding BinQuality}"/>
<DataGridCheckBoxColumn Header="Pick" Binding="{Binding Pick}"/>
<mahApps:DataGridNumericUpDownColumn Header="Vision Station" Binding="{Binding VisionStation}" Maximum="3" Minimum="1"/>
<mahApps:DataGridNumericUpDownColumn Header="Vision IO" Binding="{Binding VisionIO}" Maximum="32" Minimum="1"/>
</DataGrid.Columns>
</DataGrid>
<Button Grid.Row="3" Grid.Column="0" x:Name="ButtonAddRow" Content="Add Row" Click="ButtonAddRow_Click"/>
<Button Grid.Row="3" Grid.Column="1" x:Name="ButtonDeleteRow" Content="Delete Row" Click="ButtonDeleteRow_Click"/>
<Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" x:Name="ButtonSaveFile" Content="Save File" Click="ButtonSaveFile_Click"/>
</Grid>
然后是绑定控件的类
[Serializable]
public partial class XmlBinDefinitionModel : ViewModelBase
{
[XmlElement("EqptID")]
private string eqptID;
public string EqptID
{
get { return eqptID; }
set
{
if (eqptID != value)
{
eqptID = value;
RaisePropertyChanged(nameof(EqptID));
}
}
}
[XmlElement("EquipOpn")]
private string equipOpn;
public string EquipOpn
{
get { return equipOpn; }
set
{
if (equipOpn != value)
{
equipOpn = value;
RaisePropertyChanged(nameof(EquipOpn));
}
}
}
private List<BinDefinitionModel> binDefinition;
public List<BinDefinitionModel> BinDefinition
{
get { return binDefinition; }
set
{
if (binDefinition != value)
{
binDefinition = value;
RaisePropertyChanged(nameof(BinDefinition));
}
}
}
}//end public partial class XmlBinDefinitionModel
public partial class BinDefinitionModel : ViewModelBase
{
[XmlElement("BinCode")]
private string binCode;
public string BinCode
{
get { return binCode; }
set
{
if (binCode != value)
{
binCode = value;
RaisePropertyChanged(nameof(BinCode));
}
}
}
[XmlElement("BinDescription")]
private string binDescription;
public string BinDescription
{
get { return binDescription; }
set
{
if (binDescription != value)
{
binDescription = value;
RaisePropertyChanged(nameof(BinDescription));
}
}
}
[XmlElement("BinQuality")]
private string binQuality;
public string BinQuality
{
get { return binQuality; }
set
{
if (binQuality != value)
{
binQuality = value;
RaisePropertyChanged(nameof(BinQuality));
}
}
}
[XmlElement("Pick")]
private string pick;
public string Pick
{
get { return pick; }
set
{
if (pick != value)
{
pick = value;
RaisePropertyChanged(nameof(Pick));
}
}
}
[XmlElement("VisionStation")]
private string visionStation;
public string VisionStation
{
get { return visionStation; }
set
{
if (visionStation != value)
{
visionStation = value;
RaisePropertyChanged(nameof(VisionStation));
}
}
}
[XmlElement("VisionIO")]
private string visionIO;
public string VisionIO
{
get { return visionIO; }
set
{
if (visionIO != value)
{
visionIO = value;
RaisePropertyChanged(nameof(VisionIO));
}
}
}
}
然后在代码后面
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(FilePath.XmlBinCode); //load the file
xmlBinDefinitionModel.EqptID = xmlDocument.DocumentElement.SelectSingleNode("EqptID").InnerText;
xmlBinDefinitionModel.EquipOpn = xmlDocument.DocumentElement.SelectSingleNode("EquipOpn").InnerText;
xmlBinDefinitionModel.BinDefinition = new List<BinDefinitionModel>();
XmlNodeList binCodeList = xmlDocument.GetElementsByTagName("BinCode");
XmlNodeList binDescriptionList = xmlDocument.GetElementsByTagName("BinDescription");
XmlNodeList binQualityList = xmlDocument.GetElementsByTagName("BinQuality");
XmlNodeList pickList = xmlDocument.GetElementsByTagName("Pick");
XmlNodeList visionStationList = xmlDocument.GetElementsByTagName("VisionStation");
XmlNodeList visionIOList = xmlDocument.GetElementsByTagName("VisionIO");
for (int i = 0; i < binCodeList.Count; i++)
{
xmlBinDefinitionModel.BinDefinition.Add(new BinDefinitionModel
{
BinCode = binCodeList[i].InnerText.Remove(0,2),
BinDescription = binDescriptionList[i].InnerText,
BinQuality = binQualityList[i].InnerText,
Pick = pickList[i].InnerText,
VisionStation = visionStationList[i].InnerText,
VisionIO = visionStationList[i].InnerText
});
}