Solved

C# - string replace helper

Posted on 2011-09-22
3
300 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

790 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