Click here to Skip to main content
15,911,715 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Comments
Sergey Alexandrovich Kryukov 22-Nov-12 2:33am    
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

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();
}
 
Share this answer
 
v4

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