Changing values in XML with non standard structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kapik
    New Member
    • Mar 2012
    • 9

    Changing values in XML with non standard structure

    Hi guys!
    I am currently writing a small project and I have to deal with xml file. The file has got following structure:

    Code:
    <project>
        <configurations>
          <configuration description>
           <graph>
            <filter id ="xx" alias="Twin">
             <settings>
    
               <property value="C:\temp" name="Temp Folder"/>
    
             </settings>
            </filter>   
           </graph>
          </configuration description>
        </configurations>
    </project>
    And though I tried every trick known to me ( & Dr. Google) i do not know how to read, delete and write the value of:

    <property value="C:\temp" name="Temp Folder"/>

    simply I want to change the "C:\temp" to smth else...

    I would be glad If somebody would help with this...

    Kapik
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I don't know how you're reading in your XML but I suggest using an XmlDocument object. This stores a hierarchy of XmlNode objects. These have an Attributes property that provides a list of XmlAttribute objects.

    Whew, lots of linking!

    Anyway, you can use the Name and Value properties of an XmlAttribute object to do what you need. You'll need to follow the hierarchy down until you find the property node, then look for an attribute with the Name of "Value". You'll then want to change the value of the Value property to whatever you want. After that, you can simply get an XML string from your XmlDocument object and overwrite the file with the new XML source.

    Let me know if you have any questions/troubles with the implementation :)

    Comment

    • kapik
      New Member
      • Mar 2012
      • 9

      #3
      Thanx for the Tip Gary :)
      I'll keep in touch, because currently I am able to get values out of one level only... I have tried setting the:

      Code:
      XmlNodeList nodeList = xmlFile.SelectNodes("/project/configurations/configuration/graph/filter/settings");
      as constant ones, but somehow it still doesn't work

      Linking might be a veerryy good Idea.
      THX, once more! :))

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        I would keep things object oriented by deserializing and editing an object and then serializing the results.
        Code:
        public static void SerializeToXml<T>(T obj, string fileName)
        {
            XmlSerializer ser = new XmlSerializer(typeof(T)); 
            ser.Serialize(fileStream, obj);
            fileStream.Close(); 
            FileStream fileStream = new FileStream(fileName, FileMode.Create); 
        }
        public static T DeserializeFromXml<T>(string xml)
        {
            T result;
            XmlSerializer ser = new XmlSerializer(typeof(T));
            using (TextReader tr = new StringReader(xml))
            {
                result = (T)ser.Deserialize(tr);
            }
            return result;
        }

        Comment

        • oussema
          New Member
          • Mar 2012
          • 1

          #5
          Code:
          MySettings.Default.Reload();
                      string str = MySettings.Default.SaveClick_ModifReponse;
          
                      XmlDocument xmlDoc = new XmlDocument();
                      string path = ConfigurationManager.AppSettings["path"];
                      XmlNodeList girlReponse = xmlDoc.GetElementsByTagName("Reponse");
                      XmlNodeList girlLib = xmlDoc.GetElementsByTagName("Libelle_Reponse");
          
                      xmlDoc.Load(path);
          
                      for (int j = 0; j < girlReponse.Count; j++)
                      {
          
                          if (girlReponse[j].Attributes["Id_Reponse"].Value =="1353")
                          {
                              girlLib[j].InnerText = _libelle_reponse ;
                              xmlDoc.Save(path);
                              MessageBox.Show("les nouvelles informations sont Modifier dans le fichier xml .");
          Last edited by PsychoCoder; Mar 7 '12, 03:48 AM. Reason: Code tags added

          Comment

          Working...