473,506 Members | 16,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replace element in Xml file

25 New Member
Hi Help..........!!
I have a xml file and need to load it, read it and replace (only in memory) the content of one of its elements.

How do I do that?????????

Many thanks

M

ps. I'm using vb.net
Nov 9 '07 #1
6 5900
jkmyoung
2,057 Recognized Expert Top Contributor
A short example is provided here
Nov 9 '07 #2
milecimm
25 New Member
Hi
Thanks for your prompt reply.
But I don't want to edit but to replace one of the elements innetText in the file while the file is loaded and before I apply the xpath expression (query).

My xml file looks more or less like this:

<Jobs>
<category>Academic</category>
<category>Academic related</category>
<category>Research</category>
<category>Technical</category>
</jobs>

I want to replace Academic Related for Acad-Related because my xpath expression that queries the xml file asks for Acad-related job category instead of Academic related (long story here to tell), but I dont want to physically change or modify the xml file.
Hope my problem is clearer this time
Many thanks
M
Nov 9 '07 #3
jkmyoung
2,057 Recognized Expert Top Contributor
It'd be pretty similar to the example, just select a single node and change the text:

Expand|Select|Wrap|Line Numbers
  1. // select node
  2. XmlNode node = xmlDoc.SelectSingleNode("/Jobs/category[.='Academic Related']");
  3. // change text of node.
  4. node.InnerText = "Acad-Related";
  5.  
Nov 9 '07 #4
milecimm
25 New Member
Thanks for your help but I dont think your answer is solving my problem.Here there is a more detailed explanation:

Xml file:
Expand|Select|Wrap|Line Numbers
  1. <Jobs>
  2. <category>
  3. <categoryname>Academic</categoryname>
  4. <categoryid>1</categoryid>
  5. </category>
  6. <category>
  7. <categoryname>Academic related</categoryname>
  8. <categoryid>2</categoryid>
  9. </category>
  10. <category>
  11. <categoryname>Research</categoryname>
  12. <categoryid>3</categoryid>
  13. </category>
  14. <category>
  15. <categoryname>Technical</categoryname>
  16. <categoryid>4</categoryid>
  17. </category>
  18. </Jobs>
  19.  
Vb.net:
Expand|Select|Wrap|Line Numbers
  1. Dim getCategory As String = 'here I get the category name that user selects. The category selected could be:::  Acad-related'
  2. Dim doc As XmlDocument = New XmlDocument
  3. doc.Load(xml_file)
  4. Dim xPath As String = "jobs/category[categoryname = " & getCategory & "]"
  5.  
  6. 'AS YOU CAN SEE FROM THE XML FILE A CATEGORY IS CALLED 'Academic related' BUT USERS COULD SELECT 'Acad-related' FROM THE DROP-DOWN LIST AVAILABLE TO THEM. I NEED TO CHANGE THAT ELEMENT AFTER I LOAD THE FILE AND  BEFORE THE XPATH QUERY IS APPLIED. OTHERWISE I WOULD BE GETTING NO RESULTS AS THE XML FILE HAS NO Acad-related CATEGORY. I SHOULD NOT EDIT THE XML FILE SO THE CHANGE HAS TO BE MADE ONLY IN MEMORY
  7.  
  8. Dim nodes As XmlNodeList = doc.SelectNodes(xPath)
  9. If nodes.Count <> 0 Then
  10.     For Each item In nodes
  11.     Dim cat_id As XmlNode = item.SelectSingleNode("categoryid")
  12.     Dim cat_id_val As String = cat_id.InnerText
  13.     Dim cat_name As XmlNode = item.SelectSingleNode("categoryname")
  14.     Dim cat_name_val As String =cat_name.InnerText
  15.     response.write ("Our job categories are:" & "<br>")
  16.     response.write(cat_name_val & " ("  & cat_id_val & ")" & "<br>")
  17.     Next
  18. end if
  19.  
Many thanks again
M
Nov 13 '07 #5
jkmyoung
2,057 Recognized Expert Top Contributor
The xpath provided earlier is case sensitive. Would have to change to
XmlNode node = xmlDoc.SelectSingleNode("/Jobs/category[.='Academic related']")

Are you not allowed to change the getCategory like so (before creating the xPath)?

If getCategory = "Acad-related" Then
getCategory = "Academic related"
end if

Or if your problem is with changing the xml file, have you tried cloning the xml data? What is the problem with the previous solution?
Nov 13 '07 #6
milecimm
25 New Member
I cannot change getCategory.

I have to change the element in the xml file before I can apply the xpath, because this xpath queries for "Academic related" jobs, NOT "Acad-related" jobs. And getCategory IS "Acad-related"!!

MY XPATH:
Dim xPath As String = "jobs/category[categoryname = " & getCategory & "]"

Thanks

M
Nov 14 '07 #7

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

Similar topics

3
4750
by: Miguel J. Jiménez | last post by:
Hi, I have the following node: <node> Some text here with lots of inside it... </node> and I would like it to transfrom it using XSLT to the following: Some text<br/> here with</br> lots...
4
2467
by: Lukasz Indyk | last post by:
hello;) i have a piece of html code, and want to replace every image on my page with this code. now i do it by replacing IMG node with SPAN node, and then setting innerHTML property of SPAN node...
22
1858
by: David Isaac | last post by:
Newbie question: I have been generally open to the proposal that list comprehensions should replace 'map', but I ran into a need for something like map(None,x,y) when len(x)>len(y). I cannot...
9
1939
by: Bernd.Moos | last post by:
Given the following XML document: <text> <p> <w>Ronaldo</w> <w>scoredw> <w>the</w> <w>1</w> <c>:</c> <w>1</w>
1
3059
by: Chi Tang | last post by:
Hi, Does anybody know where I can find samples to search an element in a subtree of an xml file and replace some new value for this element? I can only find some samples to use 'replace last...
9
3698
by: Jiho Han | last post by:
Suppose I have an xml fragment like: <mother> <child name="Bob" sex="M"/> <child name="Jane" sex="F"/> ... </mother> If I wanted to replace the <mother> element to <father> element, what is...
5
4746
by: Casey | last post by:
Hello, Can someone give me specific code to replace text on a page using server side javascript? I need to use server-side because I need the output to be recognized in the final HTML so that...
3
2470
by: gregpinero | last post by:
Hi guys, What I'm trying to do is find all instances of an acronymn such as IBM on a webpage and replace it with <acronym title="International Business Machines">IBM</acronym>. However in my...
5
1457
by: gtk | last post by:
Hello, i have an xml file, and what i want to do is to replace the value of a specific element (<umps>) with some text dynamically. More specifically, here is the xml life: <rule id =...
1
3377
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. ...
0
7220
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
7105
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
7308
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,...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.