Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
XML
<xmlfile >
    <Element name="Path">
        <Type>str</Type>
        <Values>c:\</Values>
        <Comment>....</Comment>
    </Element>
</xmlfile>


Not working XML (Added from Solution 2)
XML
<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>


XAML Snippet (Added from Solution 1)
XML
<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:

C#
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);
Posted
Updated 13-Jun-16 9:37am
v4
Comments
George Jonsson 11-Jun-16 5:12am    
Can you also add the XML that is not working? (If the XML you are showing is in fact working>
El_Codero 11-Jun-16 15:38pm    
There could be more than dozens of possibilites why it is not working. "Button with Binding/Command" To call your XmlReader Method? Also the binding to your DataGrid would be interesting.

Hi

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


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


C#
System.Data.DataSet ds = new System.Data.DataSet();
            ds.ReadXml(ofd.FileName);
            DG.DataContext = ds.Tables[0];
 
Share this answer
 
Comments
Member 12558924 13-Jun-16 9:16am    
Hi Dora and thank you for your answer.
i've try it but the name of my DataGrid is not recognized by my ViewModel class. it Show me a error message like that:"DG is not present in this context"
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[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900