1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| try
{
String[] theArray = new string[] {"utilisateur", "prenom", "fond"};
String[] r = { "agence1", "sheira", "fond1.jpg" };
String filename = "test2.xml";
FileStream f = new FileStream(HttpContext.Current.Server.MapPath(filename), FileMode.OpenOrCreate, FileAccess.ReadWrite);
if (f.Length != 0)
{
XmlDocument doc = new XmlDocument();
doc.Load(f);
XmlNode racine = doc.DocumentElement;
if (racine.NodeType == XmlNodeType.Element && racine.Name == "groupe")
{
// Get a list of elements whose names are Agence
XmlNodeList nodAgences = doc.GetElementsByTagName("agence");
// Since we will look for a specific agence, get the list of all prenom
XmlNodeList nodUsers = doc.GetElementsByTagName("utilisateur");
// Visit each agences
for(int i = 0; i < nodAgences.Count; i++)
{
if (((XmlElement)(nodAgences[i])).HasAttribute("nom")){
string nomAgence = ((XmlElement)(nodAgences[i])).GetAttribute("nom");
Response.Write(r[0] );
if (nomAgence == r[0]){
Response.Write("ok:agence ");
//for(int j = 0; j < nodUsers.Count; j++){
String prenom = nodUsers[i].InnerText;
Response.Write(prenom);
if(prenom == "she")
{
Response.Write("ok:utilisateur ");
// l'utilisateur existe déjà
string nomFond = ((XmlElement)(nodUsers[i])).GetAttribute("fond");
Response.Write(nomFond);
if (nomFond != "fond2.jpg"){
Response.Write("ok:changeFond ");
// On modifie le fond => je remplace le noeud complet
XmlElement user = doc.CreateElement("utilisateur");
user.InnerText = "Lina";
XmlAttribute attrFond = doc.CreateAttribute("fond");
attrFond.Value = "fond2.jpg";
user.SetAttributeNode(attrFond);
nodUsers.Item(i).ParentNode.ReplaceChild(user, nodUsers.Item(i));
}
}
else {
Response.Write("ok:changeUser ");
// je rajoute le noeud complet
XmlElement user = doc.CreateElement("utilisateur");
user.InnerText = "Lila";
XmlAttribute attrFond = doc.CreateAttribute("fond");
attrFond.Value = r[2];
user.SetAttributeNode(attrFond);
nodAgences.Item(i).AppendChild(user);
}
//}
}
else{
XmlElement ag = doc.CreateElement("agence");
XmlAttribute attrNom = doc.CreateAttribute("nom");
attrNom.Value = r[0];
ag.SetAttributeNode(attrNom);
XmlElement user = doc.CreateElement("utilisateur");
user.InnerText = "Lila";
XmlAttribute attrFond = doc.CreateAttribute("fond");
attrFond.Value = r[2];
user.SetAttributeNode(attrFond);
ag.AppendChild(user);
racine.InsertAfter(ag, racine.FirstChild);
}
}
}
f.Close();
doc.Save(HttpContext.Current.Server.MapPath(filename));
}
f.Close();
}
else
{
XmlTextWriter xmlw = new XmlTextWriter(f, System.Text.Encoding.UTF8);
xmlw.Formatting = Formatting.Indented;
xmlw.WriteStartDocument();
xmlw.WriteStartElement("groupe");
xmlw.WriteStartElement("agence");
xmlw.WriteAttributeString("nom", r[0]);
xmlw.WriteStartElement("utilisateur");
xmlw.WriteAttributeString("fond", r[2]);
xmlw.WriteString(r[1]);
xmlw.WriteEndElement();
xmlw.WriteEndElement();
xmlw.WriteEndElement();
xmlw.WriteEndDocument();
xmlw.Flush();
//vide le buffer
xmlw.Close();
// Close the writer
f.Close();
}
}
finally {
} |
Partager