Problème d'écriture dans un fichier XML

baptaker Messages postés 8 Date d'inscription vendredi 28 novembre 2008 Statut Membre Dernière intervention 24 août 2009 - 20 août 2009 à 15:32
baptaker Messages postés 8 Date d'inscription vendredi 28 novembre 2008 Statut Membre Dernière intervention 24 août 2009 - 24 août 2009 à 09:24
Salut a tous,
alors voila j'ai un probleme mon but est d'écrirer dasn un fichier XML des commande respectant cette syntaxe la:

<command id="dd05c958-0108-402b-b2fd-f0657ed859bd" lastWriteTime="633863754488772540">
      <state>10</state>
    </command>


Malheureusement je n'arrive a écrire que de cette façon:

<command2> id="ca55a996-b5a3-494c-9022-055d14720c32" lastWriteTime="633863789706810434"<state2>0</state2></command2>



avec ce code la :
            GuId = System.Guid.NewGuid();

            XmlDocument doc = new XmlDocument();
            doc.Load(XmlCommandPath);

            XmlElement newElem = doc.CreateElement("command2");
            newElem.InnerText = " id="" + GuId.ToString() + "" lastWriteTime="" + System.DateTime.Now.Ticks.ToString()+""";
            
                      
            XmlElement newElem2 = doc.CreateElement("state2");
            newElem2.InnerText = state.ToString();
            newElem.AppendChild(newElem2);
            
            doc.DocumentElement.AppendChild(newElem);
            XmlTextWriter writer = new XmlTextWriter(XmlCommandPath, null);
            writer.Formatting = Formatting.Indented;
            doc.Save(writer);




Quelqu'un pourrait-il me dire ce qui ne va pas.

Merci d'avance

4 réponses

jamesbidon Messages postés 8 Date d'inscription vendredi 25 janvier 2008 Statut Membre Dernière intervention 8 décembre 2009
20 août 2009 à 19:44
Essaye quelque chose comme ça ( je n'ai pas tenté de compiler , mais a part des petites erreurs de frappes que je n'ai pas vu , ça devrais marcher)

XmlTextWriter writer = new XmlTextWriter(XmlCommandPath, null);
writer.Formatting = Formatting.Indented;

writer.WriteStartElement("command2");
writer.WriteAttributeString("id" , GuId.ToString());
writer.WriteAttributeString("lastWriteTime" , System.DateTime.Now.Ticks.ToString());
  
  writer.WriteStartElement("state2");
  writer.WriteString("id" , state.ToString());
  writer.WriteEndElement();

writer.WriteEndElement();
writer.Flush();
writer.Close();
0
baptaker Messages postés 8 Date d'inscription vendredi 28 novembre 2008 Statut Membre Dernière intervention 24 août 2009
21 août 2009 à 09:18
Merci,
avec cette méthode ça m'a l'air de fonctionner.
Par contre le hic c'est que ça efface tout mon fichier xml, alors que moi je veux juste y rajouter ce bloc. Je dois vraiment garder ce qui est dedans.
0
baptaker Messages postés 8 Date d'inscription vendredi 28 novembre 2008 Statut Membre Dernière intervention 24 août 2009
21 août 2009 à 09:37
j'ai oublié de dire qu'il faut que je rajoute l'élément "command"

de manière a ce que les "command" s'ajoute dans "commands"comme ceci:


<?xml version="1.0" encoding="iso-8859-1"?>
<list id="00000000-0000-0000-0000-000000000000">
  <commands id="00000100-0001-0000-0000-000000000000" lastWriteTime="633863754484710014">
    <command id="00000100-0000-0000-0000-000000000001" lastWriteTime="633863752967512804" hidden="">
      <state>100</state>
      Sirius.Action.Clean.Command
      <owner>00000001-0000-0000-0000-000000000000</owner>
      <computerOwner>d58762f4-bd8b-4679-acf0-89732042f206</computerOwner>
      <dateCreation>633821460271148087</dateCreation>
      <dateExecution>0</dateExecution>
      <dateCompletion>633863752964387784</dateCompletion>
      <scheduleType>5</scheduleType>
      <scheduleFrequency>1</scheduleFrequency>
      <scheduleDateFirstExecution>633820896000000000</scheduleDateFirstExecution>
      <scheduleDateNextExecution>633863772000000000</scheduleDateNextExecution>
      <scheduleDateStopExecution>0</scheduleDateStopExecution>
      <schedulePlanning>
      </schedulePlanning>
      <scheduleActive>False</scheduleActive>
      <repeatType>0</repeatType>
    </command>
    <command id="00000100-0000-0000-0000-000000000002" lastWriteTime="633863753089388584" hidden="">
      <state>100</state>
      Sirius.Action.Clean.TempFile
      <owner>00000001-0000-0000-0000-000000000000</owner>
      <computerOwner>d58762f4-bd8b-4679-acf0-89732042f206</computerOwner>
      <dateCreation>633821460271148087</dateCreation>
      <dateExecution>0</dateExecution>
      <dateCompletion>633863753089232333</dateCompletion>
      <scheduleType>5</scheduleType>
      <scheduleFrequency>1</scheduleFrequency>
      <scheduleDateFirstExecution>633820896000000000</scheduleDateFirstExecution>
      <scheduleDateNextExecution>633863772000000000</scheduleDateNextExecution>
      <scheduleDateStopExecution>0</scheduleDateStopExecution>
      <schedulePlanning>
      </schedulePlanning>
      <scheduleActive>False</scheduleActive>
      <repeatType>0</repeatType>
    </command>
  </commands>
</list>
0
baptaker Messages postés 8 Date d'inscription vendredi 28 novembre 2008 Statut Membre Dernière intervention 24 août 2009
24 août 2009 à 09:24
J'ai résolu pas mal de probléme. Sauf un, je n'arrive pas à écrire dans l'"élément" commands. Comment puis-je faire.
0
Rejoignez-nous