阅读XML文件的一部分,并使用MVVM将其写入datagrid C#WPF [英] Read part of XML file and write it in datagrid C# WPF using MVVM

查看:82
本文介绍了阅读XML文件的一部分,并使用MVVM将其写入datagrid C#WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HEllo,

i是一个XML文件,想要在DataGrid中读取这个XML文件的一部分。

i在互联网上寻找一些解决方案但找不到最好的方法。我使用Button with Binding / Command。我尝试为一个XML文件工作而不是所有具有相同Part的XML文件。我不明白为什么。

只需要你的帮助。非常感谢



XML文件如下:

HEllo,
i 've a XML file and want to read a part of this XML file in the DataGrid.
i have looking for some solution in Internet but not find the best way to do it. i use the Button with Binding/Command. what i try work for one XML file and not for all XML file with the same Part. i don't understand why.
just Need your help. thank a lot

the XML file look like:

<xmlfile >
    <Element name="Path">
        <Type>str</Type>
        <Values>c:\</Values>
        <Comment>....</Comment>
    </Element>
</xmlfile>





不工作的XML(从解决方案2中添加)



Not working XML (Added from Solution 2)

<xmlfile>
      <Element name="PATH1">
        <Type>int</Type>
        <Values>D:\</Values>
        <Comment>...</Comment>
      </Element>
      <Element name="PATH2">
        <Type>str</Type>
        <Values>D:\</Values>
        <Comment>...</Comment>
      </Element>
</xmlfile>

< br $>


XAML片段(从解决方案1中添加)



XAML Snippet (Added from Solution 1)

<DataGrid x:Name="DG" ItemsSource="{Binding model.Element}" Grid.Column="2" Grid.Row="3" AutoGenerateColumns="False"  CanUserAddRows="True" CanUserDeleteRows="True" SelectedItem="{Binding model.SelectedParameter, Mode=TwoWay, Converter={StaticResource ParameterConverter}}" >





我的尝试:





What I have tried:

XmlReader reader = XmlReader.Create(ofd.FileName);
reader.ReadToDescendant("Element");
XmlSerializer serializer = new XmlSerializer(typeof(Element));
Element element = new Element();
element = (Element)serializer.Deserialize(reader.ReadSubtree());
model.Element.Add(element);

推荐答案





我无法复制您的代码,因为您有自定义类..以下





Hi

I could not replicate ur code as u got custom class..try following


<DataGrid Name="DG" ItemsSource="{Binding}" />







System.Data.DataSet ds = new System.Data.DataSet();
            ds.ReadXml(ofd.FileName);
            DG.DataContext = ds.Tables[0];


如果您的型号用你的xml值正确填充,你可以阅读这篇CP文章,解释如何将WPF Datagrid绑定到模型。



MVVM和WPF DataGrid [ ^ ]
if your model is filled up correctly with your xml values, you may read this CP article that explains how to bind a WPF Datagrid to a model.

MVVM and the WPF DataGrid[^]


这篇关于阅读XML文件的一部分,并使用MVVM将其写入datagrid C#WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆