473,569 Members | 2,880 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 5903
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>Acade mic</category>
<category>Acade mic related</category>
<category>Resea rch</category>
<category>Techn ical</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.SelectSi ngleNode("/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
4758
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 of</br> inside it... Being <br/> HTML tags and not simple text like &lt;br/&gt;
4
2479
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 with my html code: var nodeToReplaceWith = document.createElement("<SPAN>"); nodeToReplaceWith.innerHTML = HTMLCodeToReplaceWith;...
22
1869
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 it seems use 'zip' because I'll lose info from x. How do I do this as a list comprehension? (Or, more generally, what is the best way to do this...
9
1942
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
3064
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 child or replade first child' but couldn't find sample for replace a child neither first nor last child. Thanks for any help, CT
9
3709
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 the fastest or most efficient way to do this?
5
4752
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 google can index it. Here is a specific example of what I want to do: <div id=SomeText> Here is some text. I went to the baseball game </div>
3
2472
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 code below it replace the <, and > with &lt; and &gt;. Thus it replaces IBM with: &lt;acronym title="International Business...
5
1462
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 = "onlyFirstName"> <tag>FirstName=''; userID=''</tag> <one-of>...
1
3383
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. The problem is when it creates transparent PNG format image then and it pixel ate the image. e.g. If i am using a gradient in background then it...
0
7930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
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 we have to send another system
0
950
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.