473,608 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find and Replace a text in xml file

5 New Member
Hi all,

I am having trouble finding and replacing text in a xml file. My issue is I have to find a text in a xml file and change it to some other text. But the text in some element tag for ex: 'Name' should not be changed.

So, for example the xml is as shown below and I had to find and replace the text 'Hello' with 'Hi' but with out changing the Name tag.

<Book>
<Name> Hello Book</Name>
<Descr>Hello there </Descr>
<Price>15</Price>
<Author>Hello author </Author>
</Book>

The result should be

<Book>
<Name> Hello Book</Name>
<Descr>Hi there </Descr>
<Price>15</Price>
<Author>Hi author </Author>
</Book>

Can anyone give me some suggestions on how to do it? I have searched so many places but couldn't get any help.


Thanks in advance,
Kushal
Jul 25 '07 #1
7 38213
kushaldutt
5 New Member
I am trying to resolve this issue in c# .Net.
Jul 25 '07 #2
jkmyoung
2,057 Recognized Expert Top Contributor
Is there only one exception to the rule or many?
eg are you trying to change all "Hello"s in text, or just the one in //Book/Descr?
Are you changing all "Hello"s but //Book/Name?

It seems you would have to have some sort of DOM model for this. Another possible way would be to use XSLT, and use something like:
if test="contains( Descr, 'Hello')"
replace with substring-before(Descr, 'Hello') + 'Hi' + substring-after(Descr, 'Hello')

I would decide how you are going to do this first.
Jul 26 '07 #3
kushaldutt
5 New Member
Is there only one exception to the rule or many?
eg are you trying to change all "Hello"s in text, or just the one in //Book/Descr?
Are you changing all "Hello"s but //Book/Name?

It seems you would have to have some sort of DOM model for this. Another possible way would be to use XSLT, and use something like:
if test="contains( Descr, 'Hello')"
replace with substring-before(Descr, 'Hello') + 'Hi' + substring-after(Descr, 'Hello')

I would decide how you are going to do this first.

I want to change all "Hello"s but //Book/Name. I am planning to use any of the .Net libraries like 'XmlDocument' class or XmlReader class or something like that.

I am planning to do like this

XmlTextReader reader = new XmlTextReader(s FileName);
reader.Whitespa ceHandling = WhitespaceHandl ing.None;
while (reader.Read())
{
switch (reader.NodeTyp e)
{
case System.Xml.XmlN odeType.Element :
{
if (reader.Name.Eq uals("Name"))
{
// Here I dont want to replace the string Hello.

}
else
{
//Replace Hello in this section.
}
}
}


The problem I am facing right now is I am able to change the string using the above approach but I am finding it hard to write it back to the xml file again.
If you feel this approach is not correct, please give me suggestions about other approaches.

I really appreaciate your help.

Thanks,
Kushal
Jul 26 '07 #4
kushaldutt
5 New Member
I solved this issue. I parsed XML using DOM tree and replaced the text value.

This tutorial helped me a lot.

http://www.quepublishi ng.com/articles/article.asp?p=2 9800&seqNum=6&r l=1


Thanks for all your help.


Thanks,
Kushal
Jul 26 '07 #5
jkmyoung
2,057 Recognized Expert Top Contributor
Another possible way I have seen (but possibly inefficient) is to use a XmlDocument object, eg

XmlDocument doc = new XmlDocument();
doc.Load(sFileN ame)

// get all text nodes
XmlNodeList textNodes = doc.SelectNodes ("//text()");
String find = new String("Hello") ;
String replace = new String("Hi");
int fLength = find.Length();
int index;
for (i = 0; i < textNodes.lengt h; i++){
if (textNodes[i].Parent().Name( ) != "Title") // THE EXCEPTION TO RULE
for (index = textNodes[i].Value.IndexOf( find); index != -1; index = textNodes[i].Value.IndexOf( find))
textNodes[i].Value = textNodes[i].Value.Substrin g(0,index)+ replace + textNodes[i].Value.Substrin g(index+fLength ); //replace each occurrence
}
Jul 26 '07 #6
kushaldutt
5 New Member
Another possible way I have seen (but possibly inefficient) is to use a XmlDocument object, eg

XmlDocument doc = new XmlDocument();
doc.Load(sFileN ame)

// get all text nodes
XmlNodeList textNodes = doc.SelectNodes ("//text()");
String find = new String("Hello") ;
String replace = new String("Hi");
int fLength = find.Length();
int index;
for (i = 0; i < textNodes.lengt h; i++){
if (textNodes[i].Parent().Name( ) != "Title") // THE EXCEPTION TO RULE
for (index = textNodes[i].Value.IndexOf( find); index != -1; index = textNodes[i].Value.IndexOf( find))
textNodes[i].Value = textNodes[i].Value.Substrin g(0,index)+ replace + textNodes[i].Value.Substrin g(index+fLength ); //replace each occurrence
}

This also could be the possible solution but we can even do this with out having to have two for loops. Read the text value to a string in the first for loop and then use str.replace method to replace all the occurences of the 'Hello' in the text value of the node. Finally, write the replaced string back to the node by giving textNodes[i].value = newString.

Anyways. Thanks for your help.
Jul 26 '07 #7
Kasty88
1 New Member
Hey,

If you were able to find a solution to this, can you please share your script here? URGENT!!

Thanks!!
Oct 12 '11 #8

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

Similar topics

3
5106
by: kittykat | last post by:
Hi, I was wondering if you could help me. I am writing a program in C++, and the problem is, i have very limited experience in this language. I would like my user to enter a specific pattern, and I want my program to search a text file for this pattern, and let the user know if this pattern exists or not. So far, i have figured out how to make my prgram read the text file, but i'm not sure how to take the information the user inserts...
8
4027
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had problems getting my algorithm to work and in order to help me find the solution I decided to print each line to screen as I read them. Then, to my surprise, I noticed that there was a space between every character as I outputted the lines to the...
1
1680
by: Joaquim Pinto | last post by:
Hi All, I'm using a richtextbox to find a particular string on a text file. Each line has more then one string separated by comas, like this: 11-11-2004, 08:30:00, 02:56:00, 04:00 I want to find a particular date, which I can do by using the find method and then replace the following strings with another ones.
2
8071
by: Paul | last post by:
Can anyone show me a good refined way to find and replace a portion of text held in a text file held on a web server? Thanks in Advance
2
1866
by: cewyattjr | last post by:
In a mediumtext field I have many instances of a URL, eg: 'http://myurl.com/index.php' that I want to replace with just '/' How can I write an update query that will update all instances of the URL in all records with this field? eg: select id, fulltext from content LIKE '%http://myurl.com/index.php%'
13
4173
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them from the html page (replace the words in the html page with blank space) I'm new to python and could use a little push in the right direction, any ideas on how to implement this? Thanks!
1
2861
by: Lengara | last post by:
I have an Access form that will be FTPing information. The FTP info will be using a text file located on the hard drive. This text file will have symbolics in it that will be replaced by entries on the Access form. I am looking for code to open a text file in the background and run a find/replace based on fields on the form. Thanks.
5
8341
by: neeludhiman | last post by:
Hi All, Can someone please help me with the code in C / C++ to find a string in an input text file and replace it with another string in output text file. The catch is that white spaces in the input file should be retained in output file. And, if possible we should be able to replace multiple search strings with multiple replace strings. e.g; srch1, srch2, srch3 in in.txt should be replaced by rplc1, rplc2, rplc3 in out.txt Thanks.
6
4968
by: Ramesh | last post by:
Hello, I am using the ofstream class to create a text file with keys and values like: Key1=Value10 Key2=Value15 Key3=Value20 In case I need to set a new value for Key2, say value50 - I am able to
2
7162
by: anii | last post by:
Hi there, I've got this piece of code, but I'm having trouble getting it to write the changes into the file. What I'm trying to do is: Read text file Find user specified character (eg. what to replace all the a's) Replace the character (eg. replace all a's with b's) Write to the same text file
0
8067
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8501
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8157
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8349
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6820
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6015
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3967
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2477
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
1336
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.