Reputation: 23
HTML string as a JSON output using C#.
I want to replace every instance of ", that is between HTML wrapper < and > with ', while converting to JSON. At the same time if it has text with " quote then it should not be replaced with the ' quote. E.g. "since the 1500s"
Code - Which is replacing all " to ' quotes
public string Content
{
get
{
return _content;
}
set
{
if (value != null)
{
this._content = this._content.Replace("\"", "'");
}
}
}
I am getting string in this form, from my View.
E.g. model.Content = "<p> Lorem Ipsum is simply dummy text of the printing <span id= "#" ids= "#" display= "inline" ></ span > and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever "since the 1500s".<br></p>";
I am using JsonConvert.SerializeObject
string output = JsonConvert.SerializeObject(model, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
Expected string - All double quotes in the HTML tags should be converted into single quote but the text quotes should be as it is using C#
"content": "<p><b>Lorem Ipsum</b> is simply dummy <i>text </i>of the printing <span id='#' ids='#' display='inline'></span> and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever \"since the 1500s\".</p>",
Upvotes: 2
Views: 5332
Reputation: 764
I think you want to replace every instance of ", that is between a < and a > with '.
So, you look for each " in your string, look behind for a <, and ahead for a >. The regex looks like:
(?<=\<[^<>]*)"(?=[^><]*\>)
So you could use
outputString = Regex.Replace(inputString, "(?<=\\<[^<>]*)\"(?=[^><]*\\>)", "'");
And then you want escape other " in your string. For that you could use
outputString = outputString.Replace(@"""", @"\""");
OR
outputString = outputString.Replace("\"", "\\\"");
I create a console application to test this ,
Console.WriteLine("Enter The String : ");
string input = Console.ReadLine();
string pattern = "(?<=\\<[^<>]*)\"(?=[^><]*\\>)";
string output = Regex.Replace(input, pattern, "'");
output = output.Replace(@"""", @"\""");
Console.WriteLine(output);
Console.ReadKey();
Input string is the string you provided and then the output will be,
<p> Lorem Ipsum is simply dummy text of the printing <span id= '#' ids= '#' display= 'inline' ></ span > and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever \"since the 1500s\".<br></p>
Upvotes: 1
Reputation: 528
Better replace all double(") by (\"). This way you can store the double quote as a part of string. Foe example :
string storeValue = "Subash \"Kharel\"";
Upvotes: 0