473,664 Members | 3,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parse and replace element in an xml file

4 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. <rule id = "onlyFirstName">
  2.         <tag>FirstName=''; userID=''</tag>
  3.         <one-of>
  4.             <item><umps>PersonalDetails:FirstName</umps><tag>FirstName=<umps>PersonalDetails:FirstName</umps>; userID=<umps>userID</umps></tag></item>
  5.         </one-of>
  6. </rule>
and here is what i want to do:

Expand|Select|Wrap|Line Numbers
  1. <rule id = "onlyFirstName">
  2.         <tag>FirstName=''; userID=''</tag>
  3.         <one-of>
  4.             <item><umps>new_Data</umps><tag>FirstName=<umps>another_new_Data_dynamically</umps>; userID=<umps>another_new_one</umps></tag></item>
  5.         </one-of>
  6. </rule>
I'm trying to replace the content of the "umps" element with another text. I mean, i don't care about the current content of the umps element.

I tried to use Regular expressions , but i found difficulty in managing it.

Any help??
Nov 22 '07 #1
5 1463
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Are you trying to do this by parsing the string or by using the DOM model?

using the DOM model will be a lot easier.

The XmlDocument Class is what you would need for this.

If you face issues with searching for examples using the XmlDocument do get back.
Nov 22 '07 #2
gtk
4 New Member
By parsing the string.
Now, i'm trying to do it with Xml classes like XmlTextReader, XmlNodeType, but i have been confused.

Which is the best way to edit xml node values dynamically? i mean, i don't want to search for the substring "<umps>Personal Details:FirstNa me</umps>" but each <umps>...</umps>
Nov 22 '07 #3
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
hi
i have not yet used the XMLReader....i think thats based on the SAX model

i have written a code snippet which works for your xml type.

Expand|Select|Wrap|Line Numbers
  1. System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
  2.             doc.LoadXml("<rule id = 'onlyFirstName'><tag>FirstName=''; userID=''</tag><one-of><item><umps>PersonalDetails:FirstName</umps><tag>FirstName=<umps>PersonalDetails:FirstName</umps>; userID=<umps>userID</umps></tag></item></one-of></rule>");
  3.  
  4.             System.Xml.XmlNode ruleNode = doc.GetElementsByTagName("rule")[0];
  5.             System.Xml.XmlNode umpsNode = ruleNode.SelectSingleNode("one-of/item/umps");
  6.  
  7.             MessageBox.Show(umpsNode.InnerText);
  8.             umpsNode.InnerText = "new value";
  9.             MessageBox.Show(umpsNode.InnerText);
  10.             MessageBox.Show(doc.InnerXml);
Please note that when loading you might have to use the Load Method instead of the LoadXML ifyou are loading the xml from a streat / file !
Nov 22 '07 #4
gtk
4 New Member
thank you for your help.
One more question: if i dont know anything about the exact position of the <umps> tag ( e.g.we don't know the location "one-of/item/umps" as we previously did) in the xml file, is it possible to accomplish the same task ?

Thanks in advance
Nov 22 '07 #5
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
yes, you get all the nodes of umps tags
or all nodes whose parent node is one-of

thats where expath comes in
the code whcich says tag/one-of/umps is xpath

take some tutorails on xpath and it will get clearer
Nov 22 '07 #6

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

Similar topics

13
4177
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!
29
2885
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
1
64094
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
6
2968
by: =?Utf-8?B?RGF2aWRN?= | last post by:
Hello, I have an XML file generated from a third party application that I would like to parse. Ideally, I plan on having a windows service setup to scan various folders for XML files and parse the file, then spit out totals. Since I haven't worked with XML too much in C#, I'm trying to develop a structured and easy-to-read way to parse the file. Essentially, I would like to read the file and add the "BatchTktAmountfor any...
0
8437
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
8861
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...
0
8636
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
7375
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
6187
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
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
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
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
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.