473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Changing values in XML with non standard structure

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

Expand|Select|Wrap|Line Numbers
  1. <project>
  2.     <configurations>
  3.       <configuration description>
  4.        <graph>
  5.         <filter id ="xx" alias="Twin">
  6.          <settings>
  7.  
  8.            <property value="C:\temp" name="Temp Folder"/>
  9.  
  10.          </settings>
  11.         </filter>   
  12.        </graph>
  13.       </configuration description>
  14.     </configurations>
  15. </project>
  16.  
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
Mar 5 '12 #1

✓ answered by r035198x

I would keep things object oriented by deserializing and editing an object and then serializing the results.
Expand|Select|Wrap|Line Numbers
  1. public static void SerializeToXml<T>(T obj, string fileName)
  2. {
  3.     XmlSerializer ser = new XmlSerializer(typeof(T)); 
  4.     ser.Serialize(fileStream, obj);
  5.     fileStream.Close(); 
  6.     FileStream fileStream = new FileStream(fileName, FileMode.Create); 
  7. }
  8. public static T DeserializeFromXml<T>(string xml)
  9. {
  10.     T result;
  11.     XmlSerializer ser = new XmlSerializer(typeof(T));
  12.     using (TextReader tr = new StringReader(xml))
  13.     {
  14.         result = (T)ser.Deserialize(tr);
  15.     }
  16.     return result;
  17. }
  18.  

4 1817
GaryTexmo
1,501 Expert 1GB
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 :)
Mar 5 '12 #2
kapik
9
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:

Expand|Select|Wrap|Line Numbers
  1. XmlNodeList nodeList = xmlFile.SelectNodes("/project/configurations/configuration/graph/filter/settings");
  2.  
as constant ones, but somehow it still doesn't work

Linking might be a veerryy good Idea.
THX, once more! :))
Mar 5 '12 #3
r035198x
13,262 8TB
I would keep things object oriented by deserializing and editing an object and then serializing the results.
Expand|Select|Wrap|Line Numbers
  1. public static void SerializeToXml<T>(T obj, string fileName)
  2. {
  3.     XmlSerializer ser = new XmlSerializer(typeof(T)); 
  4.     ser.Serialize(fileStream, obj);
  5.     fileStream.Close(); 
  6.     FileStream fileStream = new FileStream(fileName, FileMode.Create); 
  7. }
  8. public static T DeserializeFromXml<T>(string xml)
  9. {
  10.     T result;
  11.     XmlSerializer ser = new XmlSerializer(typeof(T));
  12.     using (TextReader tr = new StringReader(xml))
  13.     {
  14.         result = (T)ser.Deserialize(tr);
  15.     }
  16.     return result;
  17. }
  18.  
Mar 6 '12 #4
Expand|Select|Wrap|Line Numbers
  1. MySettings.Default.Reload();
  2.             string str = MySettings.Default.SaveClick_ModifReponse;
  3.  
  4.             XmlDocument xmlDoc = new XmlDocument();
  5.             string path = ConfigurationManager.AppSettings["path"];
  6.             XmlNodeList girlReponse = xmlDoc.GetElementsByTagName("Reponse");
  7.             XmlNodeList girlLib = xmlDoc.GetElementsByTagName("Libelle_Reponse");
  8.  
  9.             xmlDoc.Load(path);
  10.  
  11.             for (int j = 0; j < girlReponse.Count; j++)
  12.             {
  13.  
  14.                 if (girlReponse[j].Attributes["Id_Reponse"].Value =="1353")
  15.                 {
  16.                     girlLib[j].InnerText = _libelle_reponse ;
  17.                     xmlDoc.Save(path);
  18.                     MessageBox.Show("les nouvelles informations sont Modifier dans le fichier xml .");
Mar 6 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: martin | last post by:
I needed a way to display calculated, multiple, changing values (numerical sums) as users interacted with the page, and do this without going back to the server to load the page again. What I...
6
by: spmm_pls | last post by:
Hi, Could some1 please tell me what the best way is to adjust the value of a column in my databound datagrid (in ASP.NET) For example my database returns the values 1,2 and 3 in the column...
1
by: Peter Rilling | last post by:
Okay, I have a structure where I want to initialize the fields to some special value, but I cannot initialize them directly (e.g. private bool myBool = false) and I cannot create a defauld...
6
by: Polska, Ursula | last post by:
Hello experts, could explain to initialize a structure? Private Structure sEG Public EN As Integer = 1 Public EG1 As Integer = 2 Public EG9 As String = "EG, 14.2" End Structure Please no...
0
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing...
0
by: Anthony Liu | last post by:
I am talking about the 'take' method of numarray. See here for more info: http://stsdas.stsci.edu/numarray/Doc/node33.html If I initialize a matrix 'mymatrix' by zeros((3,3),...
2
by: mmcquade.forums | last post by:
I've built a CROSSTAB query that will list attendance for classes. Since the ATTENDANCE table tracks if someone was ABSENT with a yes/no field, the results of the crosstab show "-1" if checked. ...
3
by: sbaird | last post by:
Aloha from Hawaii, I'm beating my head on the wall here. I have a recruiting contact managment database I'm trying to create. Managers (there ar 14 of them) have to make a certain number of...
6
by: nagar | last post by:
My application connects to a SQLite db and gets a number of fields which are displayed in a list though a typed dataset. I need to manually update an unbound column (which contains the index of...
0
by: yogi_bear_79 | last post by:
I am a beginner. I am supposed to use a switch structure. I wrote the code below, which works great, but I can't come up with a reasonable usage of the switch structure for this program. Any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.