Solved

finding a string and replacing

Posted on 2013-11-18
5
205 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

Industry Leaders: 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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