Solved

finding a string and replacing

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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