2

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&nbsp;<span id='#' ids='#' display='inline'></span> and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever \"since the 1500s\".</p>",
5
  • 1
    Can you provide more information, please. Why should double quotes become single quotes, for example? How are you converting it to JSON, etc? Commented Dec 28, 2017 at 6:19
  • Is a method of c# returning this output? please more info Commented Dec 28, 2017 at 6:20
  • Are you using JSON.NET or JavaScriptSerializer or any other Serializer? Commented Dec 28, 2017 at 6:37
  • @Jack it would be useful to update your question with the code, otherwise that is just a mess. ps also try to put some effort into formatting it Commented Dec 28, 2017 at 7:49
  • Updated the question Commented Dec 28, 2017 at 8:26

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Yes you are right, but this expression is not working
I updated the answer with a example console app. Check that
0

Better replace all double(") by (\"). This way you can store the double quote as a part of string. Foe example :

string storeValue = "Subash \"Kharel\"";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.