Solved

finding a string and replacing

Posted on 2013-11-18
5
199 Views
Last Modified: 2013-11-19
Hi,

I have just taken over a project from another engineer and the system he was using stored data results in XML files, however I need to go through and replace a certain data string with another based on an pre-requisite.  As I'm learning C# at the moment I figured it might be a nice little learning project at the same time.

The two XML nodes I'm looking at are Number and the Terminal address
What we are doing is looking at the (Host)Number (In this case 000421) and then changing the IP address to the new address based on a new criteria.   Now in theory this would be a simple user find and replace task however with over 100,000 results and multiple IP's for each (Host)Number that's carpel tunnel syndrome for anyone who attempts it plus new ones are being generated every day so its a but of an uphill struggle.

  XML snippet
<Transfer>
<TransferDateTime>2013-05-02T12:52:29.968</TransferDateTime>
<Terminal address="1.2.3.4" Port="76"/>
<Ident>
<Number>000421</Number>
<other irrelevant data>
</Ident>
</Transfer>

Open in new window


So far I have a very simple bit of code that checks the root folder then creates a subfolder for each (Host)Number it finds and puts all the matching files into there no problems,  what I would then like to do is pull out all the  Terminal address and replace them however I assumed I could use the same code but as its not the same type of data (if that's the right wording) where as the nodes are easy to find with

  XDocument doc = XDocument.Load(@"location\file.xml");

            var Host = doc.Descendants("number");

            foreach (var number in Host)
            {
                Console.WriteLine(number.Value);
            }
            Console.ReadLine();

Open in new window



however trying to get the  Terminal address is eluding me a bit, If i change the node to Transfer if brings back everything except the Terminal address and Port.

Any help would be greatly appreciated.
0
Comment
Question by:Litation
  • 3
  • 2
5 Comments
 

Accepted Solution

by:
Litation earned 0 total points
ID: 39657253
Never mind I think I cracked it using
XmlDocument doc = new XmlDocument();
doc.Load(@"location\file.xml");

XmlNodeList aNodes = doc.SelectNodes("/Transfer/Terminal");


foreach (XmlNode aNode in aNodes)
{
      XmlAttribute idAttribute = aNode.Attributes["address"];

   
   if (idAttribute != null)
   {
           string currentValue = idAttribute.Value;
            idAttribute.Value = "newvalue";
}

Open in new window

0
 

Author Closing Comment

by:Litation
ID: 39657254
Solved it myself.
0
 
LVL 5

Expert Comment

by:allanau20
ID: 39657810
Hi Litation.

Just a kind fyi..

Line #2: you can remove the @ unless you want to span to another line like this:
doc.Load(@"location\
           file.xml");

Open in new window


hope this helps.
0
 

Author Comment

by:Litation
ID: 39661180
I thought the @ stopped the \ being a line break in C#.  I thought I would have to put location\\file.xml without the @ e.g.  'C:\\folder\\file.xml'  rather than @'c:\folder\file.xml'
0
 
LVL 5

Expert Comment

by:allanau20
ID: 39661184
you're right;
string g = "\\\\server\\share\\file.txt";   // \\server\share\file.txt
string h = @"\\server\share\file.txt";      // \\server\share\file.txt

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now