Click here to Skip to main content
12,358,497 members (63,897 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# Windows
Hi All,
I have an "Acount" and my Acount has multiple "users" .

And my user have three settings like App,Web,Key

So we have to write XML on this behalf of tree.

Structure like -

Acount
User1
Acount Setting
web
App
Key

User2
Acount Setting
web
App
Key

I have create XML like same as structure.
Posted 21-Nov-12 20:24pm
rummer955
Comments
   
First, tag your UI library. What did you try so far?
--SA
BillWoodruff 22-Nov-12 22:36pm
   
Edit your answer, and use indentation to make the tree data-structure node relationships clearer.

This is very vague: are you saying you have a tree data-structure now: whose root node is "Account," and that "Account" has a series of child-nodes, whose names start with the word "User" followed by a number, and each of those child-nodes has three child nodes, web, App, and Key ?

Your comments above do not explain what the node "Account Setting" is: is it the one and only one child node of each User#, and the "parent" of web, App, and Key ?

Your goal is to translate an existing tree data-structure to XML ?

You need to show how you build your tree structure. What type of data do: web, App, and Key hold ?

Depending on how you built your tree structure, getting it into XML may be as simple as using a JSON serialzer (such as the one by Mehdi Gholam here on CodeProject), or another XML, or JSON, serializer.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

private void saveAccountsToXMLFile(string filename)
{
    XmlDocument xDoc = new XmlDocument();
    XmlWriter xmlWriter = XmlWriter.Create(filename);
 
    xmlWriter.WriteStartDocument();
    xmlWriter.WriteStartElement("Accounts");
 
    XmlElement el = (XmlElement)xDoc.AppendChild(xDoc.CreateElement("Accounts"));
 
    foreach (Account x in AccountList)
    {
        xmlWriter.WriteStartElement("Account");
 
foreach(user usr in x.Users)
{
    xmlWriter.WriteStartElement("User");
 
    xmlWriter.WriteStartElement("Username");
    xmlWriter.WriteString(usr.Username);
    xmlWriter.WriteEndElement();
 
            foreach(AccountSetting acctset in usr.AccountSettings)
            {
    xmlWriter.WriteStartElement("Account Settings");
 
                xmlWriter.WriteStartElement("Web");
    xmlWriter.WriteString(acctset.Web);
    xmlWriter.WriteEndElement();
 
    xmlWriter.WriteStartElement("App");
    xmlWriter.WriteString(acctset.App);
    xmlWriter.WriteEndElement();
 
    xmlWriter.WriteStartElement("Key");
    xmlWriter.WriteString(acctset.Key);
    xmlWriter.WriteEndElement();
 
    xmlWriter.WriteEndElement();
            }
 
            xmlWriter.WriteEndElement();
        }
xmlWriter.WriteEndElement();
}
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}
  Permalink  
v4

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100