Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# - string replace helper

Posted on 2011-09-22
3
Medium Priority
?
320 Views
Last Modified: 2012-05-12
Hello Experts...

I need help converting a string from one format to another. So, I have a bunch of xml xpath values in a datatable for processing later, for example one looks like this:
/soap:Envelop/soap:Body/ns2:findAllRolesResponse/return[2]/unscheduledWorkTOs[3]/workRangeTo/workRangeStartNumber

What I need is a little help doing a string replace so that the corresponding string looks like this one:

/*[local-name(.)='Envelope'][1]/*[local-name(.)='Body'][1]/*[local-name(.)='findAllRolesResponse'][1]/*[local-name(.)='return'][2]/*[local-name(.)='unscheduledWorkTOs'][3]/*[local-name(.)='workRangeTo'][1]/*[local-name(.)='workRangeStartNumber'][1]

I can get the first of the the string replaced using StringBuilder Replace...see attached code.

Just need help crawling through the rest of the string to change it as well.


        Any help is much appreciated.
Thanks!

static void Main(string[] args)
{
   string s = "/soap:Envelop/soap:Body/ns2:findAllRolesResponse/return[2]/unscheduledWorkTOs[3]/workRangeTo/workRangeStartNumber";

   // A
   // Create new StringBuilder from string
   StringBuilder b = new StringBuilder(s);
   Console.WriteLine(b);

   // B
   // Replace the first part
   b.Replace("/soap:Envelop/soap:Body/ns2:", "/*[local-name(.)='Envelope'][1]/*[local-name(.)='Body'][1]/*[local-name(.)='");
   Console.WriteLine(b);

}

Open in new window

0
Comment
Question by:j420exe1
[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
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 36584929
Use String.Split() to break the original string, on the '/' character, into an array of strings.  Loop through the array and remove everything up to and including the first colon - then append the result of that operation to the StringBuilder with the additional /*[local-name(.)='blah blah'][1] stuff.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{
	static void Main(string[] args)
	{
		string testString = "/soap:Envelop/soap:Body/ns2:findAllRolesResponse/return[2]/unscheduledWorkTOs[3]/workRangeTo/workRangeStartNumber";
		StringBuilder output = new StringBuilder();

		// Split string on '/'
		string[] parts = testString.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

		// Loop through each part
		foreach (string part in parts)
		{
			// Remove everything up to and including the first colon
			string temp = part.Substring(part.IndexOf(':') + 1);

			// Append /*[local-name(.)='<part here>'[1] to the string builder
			output.AppendFormat("/*[local-name(.)='{0}'[1]", temp);
		}

		Console.WriteLine("Input:");
		Console.WriteLine(testString);
		Console.WriteLine();

		Console.WriteLine("Output:");
		Console.WriteLine(output.ToString());
		Console.WriteLine();

		Console.Write("Press any key to exit...");
		Console.ReadKey();
	}
}

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 36584934
I think my format on line 23 is a little wrong - just change it as-needed.
0
 

Author Closing Comment

by:j420exe1
ID: 36587060
Thanks...that worked and it wasn't as bad as I thought.

I updated line 23:
output.AppendFormat("/*[local-name(.)='{0}'", temp);
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

671 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