There is a DataGrid . There are problems with uploading content to XML.

Class to serialize

 [Serializable] public class Buttons { public bool VisibilityBut { get; set; } public string Name { get; set; } public string PathImag { get; set; } public string PathExe { get; set; } } 

If I do serialization like this

 XmlSerializer formatter = new XmlSerializer(typeof(Buttons)); using (FileStream fs = new FileStream(@"XML.XML", FileMode.OpenOrCreate)) formatter.Serialize(fs, DataGrid1); 

then the file is created empty and an exception is thrown

System.InvalidOperationException: "Error creating XML document." InvalidCastException`: Failed to cast the object type "System.Windows.Controls.DataGrid" to the type of "Button Panel. Buttons".

And if I do this:

 if (DataGrid1.ItemsSource == null) return; var serializer = new XmlSerializer(typeof(List<Buttons>)); var writer = new StreamWriter("XML.xml"); serializer.Serialize(writer, DataGrid1.ItemsSource as List<Buttons>); writer.Close(); 

then only the XML header is created in the file, and everything else is empty, by selecting the types, the solution is this.

 ObservableCollection<ButtonEIIS> collection = null; private void butSaveXML_Click(object sender, RoutedEventArgs e) // Π½Π°ΠΆΠ°Π»ΠΈ ΠΊΠ½. "Π‘ΠΎΡ…Ρ€Π°Π½ΠΈΡ‚ΡŒ Π² XML" { var serializer = new XmlSerializer(typeof(ObservableCollection<Buttons>)); using (FileStream fs = new FileStream(@"XML.XML", FileMode.OpenOrCreate)) { serializer.Serialize(fs, DataGrid1.ItemsSource as ObservableCollection<Buttons>); } } 

in XAML binding such

 <DataGrid x:Name="DataGrid1" Margin="10,171,10,52" FontSize="14" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridCheckBoxColumn Header="Π’ΠΈΠ΄" Binding="{Binding VisBut}"/> <DataGridTextColumn Header="Надпись Π½Π° ΠΊΠ½ΠΎΠΏΠΊΠ΅ (ΠΏΠ°ΠΏΠΊΠ° подсистСмы)" Binding="{Binding NameSubEIIS}"/> <DataGridTextColumn Header="ΠŸΠΎΠ»Π½Ρ‹ΠΉ ΠΏΡƒΡ‚ΡŒ ΠΊ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ΅" Binding="{Binding PathPNG}"/> <DataGridTextColumn Header="ΠŸΠΎΠ»Π½Ρ‹ΠΉ ΠΏΡƒΡ‚ΡŒ ΠΊ запускаСмому ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡŽ" Binding="{Binding PathEXE}"/> </DataGrid.Columns> </DataGrid> 
  • And what is the problem, actually? - Andrei NOP
  • @ vova-forum Edit your answer (link "edit"), insert what you wrote in the comment. - Vadim Ovchinnikov
  • And why List<Buttons> ? - Andrei NOP
  • @ vova-forum Reformed your question, put all your efforts there. Please remove your comments, as they are irrelevant. Always call using , as you did in the first case. Also, the error in the first case draws your attention to the fact that you had to pass DataGrid1.ItemsSource instead of just DataGrid1 , and typeof(List<Buttons>) instead of typeof(Buttons) . - Vadim Ovchinnikov
  • Look under the debugger for the result of the operation DataGrid1.ItemsSource as List<Buttons> - Andrey NOP

0