Solved

C# - string replace helper

Posted on 2011-09-22
3
294 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
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now