Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am able to do the same using c# string methods but interested to know is whether we can have any regular expression for solving the same.


C#
String[] result = val.Split('>');
String temp1 = null;
for (int i = 0; i < (result.Length)-1; i++)
{
if(result[i].Contains(" ")&& result[i].Contains("<")&&!result[i].Contains("=") )
{
String[] temp = result[i].Split('<');

temp[1] = temp[1].Replace(" ", "_");
result[i] = temp[0] +"<"+ temp[1];
}
temp1 = temp1 + result[i] + ">";
}


Example

XML
<UnAvailabilityDescriptions t="String[]">
<Index0>Maintenance mode active</Index0>
<Index1>
Remote host connection lost</Index1>
<Index2>
Host NDCLink not available</Index2>
<Index3>
<local unavailability>
</local unavailability>
</Index3>
</UnAvailabilityDescriptions>


to be changed as

XML
<UnAvailabilityDescriptions t="String[]">
<Index0>Maintenance mode active</Index0>
<Index1>
Remote host connection lost</Index1>
<Index2>
Host NDCLink not available</Index2>
<Index3>
<local_unavailability>
</local_unavailability>
</Index3>
</UnAvailabilityDescriptions>



Waiting for help...........
Please do the needful.:-)
Posted
Comments
VJ Reddy 22-May-12 2:40am    
Thank you for accepting the solution :)

The Replace method of Regex class can be used to replace the white space characters with _ as shown below
C#
xmlText = Regex.Replace(
            xmlText, @"(?=<[^=]+?>)(?=</?\w+\s+\w+)(<.*?)(\s+)(.*?>)",@"$1_$3",
            RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

Where xmlText is the given xml text.

The search pattern
"(?=<[^=]+?>)(?=</?\w+\s+\w+)(<.*?)(\s+)(.*?>)"
searches white space between two words within < and > character where there is no = inside the < and > characters. However, the above replacement works to replace white spaces only at location between words.

i.e. if we have
<local unavailability local unavailability>
it will replace the first space between local and unavailability words.
<local_unavailability local unavailability>
If we ran the above second time with the replaced text then the second space will be replaced.
<local_unavailability_local unavailability>

The above pattern replaces multiple white spaces with a single _ and also it replaces only between words. So, the spaces at the beginning and at end of words remain as it is.
 
Share this answer
 
v2
Comments
Maciej Los 21-May-12 12:32pm    
Great work, my 5!
VJ Reddy 21-May-12 12:54pm    
Thank you very much, losmac :)
Shruti91 24-Jul-15 6:25am    
How to use
xmlText = Regex.Replace(
xmlText, @"(?=<[^=]+?>)(?=)",@"$1_$3",
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
this in Java?
Hi,

The below solution works out for me. After constructing the XML, I just returned the XML file as string instead of XmlDocument

For Ex:

public static string GenerateXML(string FirstName,string LastName)
{
XmlDocument xDoc = new XmlDocument();

XmlElement root = xDoc.CreateElement("Root");
xDoc.AppendChild(root);

XmlNode xNodeFirstName = xDoc.CreateElement("FirstName");
xNodeFirstName.InnerText = FirstName;
xDoc.DocumentElement.AppendChild(xNodeFirstName);

XmlNode xNodeLastName = xDoc.CreateElement("LastName");
xNodeLastName.InnerText = LastName;
xDoc.DocumentElement.AppendChild(xNodeLastName);

return xDoc.OuterXml.ToString();
}

In the main method

static void Main(string[] args)
{
string strXml=GenerateXML("John","Smith");
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXml);
}

In this way, all the whitespaces and unwanted carriage retrurn were removed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900