473,405 Members | 2,287 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,405 software developers and data experts.

Find and Replace a text in xml file

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 38135
I am trying to resolve this issue in c# .Net.
Jul 25 '07 #2
jkmyoung
2,057 Expert 2GB
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
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(sFileName);
reader.WhitespaceHandling = WhitespaceHandling.None;
while (reader.Read())
{
switch (reader.NodeType)
{
case System.Xml.XmlNodeType.Element:
{
if (reader.Name.Equals("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
I solved this issue. I parsed XML using DOM tree and replaced the text value.

This tutorial helped me a lot.

http://www.quepublishing.com/articles/article.asp?p=29800&seqNum=6&rl=1


Thanks for all your help.


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

XmlDocument doc = new XmlDocument();
doc.Load(sFileName)

// 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.length; 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.Substring(0,index)+ replace + textNodes[i].Value.Substring(index+fLength); //replace each occurrence
}
Jul 26 '07 #6
Another possible way I have seen (but possibly inefficient) is to use a XmlDocument object, eg

XmlDocument doc = new XmlDocument();
doc.Load(sFileName)

// 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.length; 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.Substring(0,index)+ replace + textNodes[i].Value.Substring(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
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
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...
8
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...
1
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...
2
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
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...
13
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...
1
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...
5
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...
6
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...
2
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.