Tout d'abord bonjour à tous, je suis tout nouveau sur ce forum, je ne sait pas si je poste dans la bonne section, veuillez m'en excuser.
Je développe actuellement un site Web en asp.net et il se trouve que j'ai un problème.
Je voudrais créer un fichier XML avec cette structure :
Et je voudrais rajouter un (ou des, le nombre de profil n'étant pas fixe) nouveau profil et ces options à la suite.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 <?xml version="1.0" encoding="utf-8"?> <Racine> <Profil id="test"> <option1>False</option1> <option2>True</option2> </Profil> <Profil id="test2"> <option1>False</option1> <option2>True</option2> </Profil> </Racine>
J'ai essayé de la manière suivante :
Avec ce code j'obtiens une structure de type :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 xmlDoc.Load(strFilename); XmlElement elmRoot = xmlDoc.DocumentElement; XmlElement elmProfil = xmlDoc.CreateElement("Profil"); XmlElement elmopt1 = xmlDoc.CreateElement("option1"); // Ajouter l'attribut Id. XmlAttribute newAttr = xmlDoc.CreateAttribute("id"); newAttr.Value = TextBoxName.Text; elmProfil.Attributes.Append(newAttr); //Noeud Profil XmlText Profil = xmlDoc.CreateTextNode("true"); elmRoot.AppendChild(elmProfil); elmRoot.LastChild.AppendChild(Profil); elmRoot.AppendChild(xmlDoc.CreateWhitespace("\r\n\t\t")); //Noeud opt1 XmlText opt1= xmlDoc.CreateTextNode(Checkboxcalldate.Checked.ToString()); elmRoot.AppendChild(elmopt1); elmRoot.LastChild.AppendChild(opt1); elmRoot.AppendChild(xmlDoc.CreateWhitespace("\r\n\t\t")); elmRoot.AppendChild(xmlDoc.CreateWhitespace("\r\n")); xmlDoc.Save(strFilename);
J'ai vraiment un problème avec le nœud profil, je le crée avec xmlElement, alors que je pense qu'il faudrait avec un xmlNode mais je n'y arrive pas. Je voudrait d'abord réussir à avoir la bonne structure.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 <Racine> <Profil id="test">true</Profil> <opt1>False</opt1> </Racine>
Je vous remercie d'avance de votre aide
Partager