2010-08-25 8 views
0

Comment puis-je créer un fichier XML avec ce format en utilisant C#?Écrire en XML en utilisant 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> 

Répondre

6

Vous pouvez jeter un coup d'œil à 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"); 

Une autre alternative est d'utiliser un XmlWriter ou XmlDocument.

+0

Merci Darin :). J'ai eu la réponse. – Shahin

+0

Par exemple Mon fichier XML existe. puis-je mettre à jour mon fichier avec ces codes? Je veux ouvrir un fichier existant et le mettre à jour. – Shahin

+0

@shaahin, oui vous pouvez mettre à jour les fichiers XML en utilisant XDocument: http://www.google.com/#hl=fr&q=update+xml+xdocument&aq=f&aqi=m1&aql=&oq=&gs_rfai=&pbx=1&fp=7db4f7af4a13aa89 –