-1

How do you convert char to XElement.value from a XML file in WPF ? I've printed the tree elements, but not their values; I get the cannot convert from 'char' to 'System.Xml.Linq.XElement' argument. Perphaps i'm iterating the wrong type ?

Thank you

    public partial class MainWindow : Window
   {
    string file1 = System.IO.File.ReadAllText(@"H:\MpxWpfComponents.strings.xsd");
    string file2 = System.IO.File.ReadAllText(@"H:\MpxWpfComponents.bg.strings.xml");
    string file3 = System.IO.File.ReadAllText(@"H:\MpxWpfComponents.fr.strings.xml");

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnOpenFile_Click1(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
        openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        if (openFileDialog.ShowDialog() == true)
        {
            foreach (string filename in openFileDialog.FileNames)
                listXML.Items.Add(Path.GetFileName(filename));
        }
        var xmlDoc = XDocument.Parse(file2);
        BuildTree(treeView.Items, xmlDoc.Elements().First());           
    }

    private void BuildTree(ItemCollection itemCollection, XElement xElement)
    {
        var item = new TreeViewItem() { Header = xElement.Name.LocalName };
        itemCollection.Add(item);
        foreach (var xElem in xElement.Elements())
        {
            BuildTree(item.Items, xElem);
        }

        item = new TreeViewItem() { Header = xElement.Value };
        itemCollection.Add(item);

        foreach (var xElem in xElement.Value)
        {
            BuildTree(item.Items, xElem);
        }
    }

CS code

XAML code

Philip
  • 63
  • 11
  • Welcome to Stack Overflow. Please see [How to Ask](https://stackoverflow.com/help/how-to-ask) for instructions on how to write a good question. As it stands, it is hard to understand exactly what you're asking about. Also, please refrain from posting links to images of code or errors. Instead, copy and paste said info into your question. This is of course a general suggestion. You should always make sure that any and all code you post constitutes a [Minimal, Complete, and Verifiable](https://stackoverflow.com/help/mcve) example of the problem you are facing. – stelioslogothetis Jul 05 '17 at 09:21
  • See posting : https://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng Jul 05 '17 at 11:22

1 Answers1

1

The Value property returns the text contents of the element. You want to iterate through the element's child elements:

private void BuildTree(ItemCollection itemCollection, XElement xElement)
{
    var item = new TreeViewItem() { Header = xElement.Name.LocalName };
    itemCollection.Add(item);
    foreach (var xElem in xElement.Elements())
    {
        BuildTree(item.Items, xElem);
    }

    item = new TreeViewItem() { Header = xElement.Value };
    itemCollection.Add(item);

    foreach (var xElem in xElement.Elements()) //<---
    {
        BuildTree(item.Items, xElem);
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88