?
Solved

finding a string and replacing

Posted on 2013-11-18
5
Medium Priority
?
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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