[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

finding a string and replacing

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
Litation
Asked:
Litation
  • 3
  • 2
1 Solution
 
LitationAuthor Commented:
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
 
LitationAuthor Commented:
Solved it myself.
0
 
allanau20Commented:
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
 
LitationAuthor Commented:
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
 
allanau20Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now