Shahin
Shahin

Reputation: 12861

Write to XML using c#

How can I create xml file with this format using c#?

<?xml version="1.0" encoding="utf-8" ?>
<wrap>
  <content>
    <title>
      WHAT ARE ROLES?
    </title>
    <text>
        Roles are security based permissions that can be assigned to registered user of the site. Users can have any number of role based security permissions that the administrator deems appropriate. Certain parts of the application may have security permissions assigned to them to make the information they contain available only to those users with the required permisions.
    </text>
  </content>
  <content>
    <title>
      CREATE A ROLE
    </title>
    <text>
        To create a new role, simply enter its name into the textbox on top and click the "Add Role" button. Once it is created, it is then available to the administrator to be used and assigned to any registered user account.
    </text>
  </content>
  <content>
    <title>
      DELETE A ROLE
    </title>
    <text>
        To delete a role, select the checkboxes on the left side of the gridview and click the "Delete Selected" button.
    </text>
  </content>
</wrap>

Upvotes: 0

Views: 325

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

You may take a look at XDocument.

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XElement("wrap",
        new XElement("content",
            new XElement("title", "WHAT ARE ROLES?"),
            new XElement("text", "Roles are security based permissions that can be assigned to registered user of the site. Users can have any number of role based security permissions that the administrator deems appropriate. Certain parts of the application may have security permissions assigned to them to make the information they contain available only to those users with the required permisions")
        ) // continue with other content tags
    ) 
);
doc.Save("test.xml");

Another alternative is to use a XmlWriter or XmlDocument.

Upvotes: 6

Related Questions