1
votes

i followed the tutorial here: http://www.wpf-tutorial.com/treeview-control/treeview-data-binding-multiple-templates/

in order to populate my treeview

and here is the screenshot of the program.

enter image description here

in my program i need to create xml based from treeview

this is how to xml output should look like

<div TYPE="BODY_CONTENT">
   <div TYPE="PARAGRAPH" ORDER="1">
      <div TYPE="TEXT">
         <fptr>
           <area name="John Doe"/>
           <area name="Jane Doe"/> 
           <area name="Sammy Doe"/>
         </fptr>
      </div>
   </div>
   <div TYPE="PARAGRAPH" ORDER="2">
      <div TYPE="TEXT">
         <fptr>
           <area name="Mark Moe"/>
           <area name="Norma Moe"/> 
         </fptr>
      </div>
   </div>
</div>

i know a little bit of how to create an xml(based from those who answer my questions here, i have ask a lot of xml related question) but i haven't experience how to create this kind of xml.

so far i only have the loop for the treeview

foreach (Family item in trvFamilies.Items)
            {  
                foreach (var itm in item.Members)
                {
                    Console.WriteLine(itm.Name);
                }
            }

in the xml. the order is based from parent of the treeview

and the values inside fptr tags came from the child node of each parent node in the treeview

3

3 Answers

1
votes

You need to read more about how to create your own XML's, there is a lot of question's about this

As well would recommend to read about XmlDocument and xmlDocument's method CreateElement that's all you need to create custom Xml's.

Edit: Sorry forget about main source for any stuff about xml serialization: link :)

0
votes

Have you tried two way binding shown in this link? Two-way binding of Xml data to the WPF TreeView Make sure your question is clear

0
votes

a while ago i ask another question. ive been looking all over for some reference and then i read the comment of jdweng and it solve my problem, here is the link

add new Element to a specific part of xml tree

XDocument doc = new XDocument(new XElement("div", new XAttribute("TYPE", "BODY_CONTENT")));
            XElement title = doc
               .Descendants("div")
               .Where(x => (string)x.Attribute("TYPE") == "BODY_CONTENT")
               .FirstOrDefault();
            int i = 1;
            foreach (Family item in trvFamilies.Items)
            {
                title.Add(new XElement("div",  new XAttribute("TYPE", "PARAGRAPH"), new XAttribute("ORDER", i.ToString())));
                XElement text = doc
                        .Descendants("div")
                        .Where(x => (string)x.Attribute("ORDER") == i.ToString())
                    .FirstOrDefault();
                title.Add(new XElement("div", new XAttribute("TYPE", "TEXT"), new XElement("fptr", new  XAttribute("order", i.ToString()))));

                foreach (var itm in item.Members)
                {
                    XElement area = doc
                      .Descendants("fptr")
                      .Where(x => (string)x.Attribute("order") == i.ToString())
                      .FirstOrDefault();
                    area.AddFirst(new XElement("area", new XAttribute("name", itm.Name)));
                }
                i++;
            }
            Console.WriteLine(doc);