Convert character entities to their unicode equivalents

前端 未结 4 1494
夕颜
夕颜 2020-12-16 15:53

I have html encoded strings in a database, but many of the character entities are not just the standard & and <. Entities like

相关标签:
4条回答
  • 2020-12-16 15:57

    My first thought is, can your RSS reader accept the actual characters? If so, you can use HtmlDecode and feed it directly in.

    If you do need to convert it to the numeric representations, you could parse out each entity, HtmlDecode it, and then cast it to an int to get the base-10 unicode value. Then re-insert it into the string.

    EDIT: Here's some code to demonstrate what I mean (it is untested, but gets the idea across):

    string input = "Something with &mdash; or other character entities.";
    StringBuilder output = new StringBuilder(input.Length);
    
    for (int i = 0; i < input.Length; i++)
    {
        if (input[i] == '&')
        {
            int startOfEntity = i; // just for easier reading
            int endOfEntity = input.IndexOf(';', startOfEntity);
            string entity = input.Substring(startOfEntity, endOfEntity - startOfEntity);
            int unicodeNumber = (int)(HttpUtility.HtmlDecode(entity)[0]);
            output.Append("&#" + unicodeNumber + ";");
            i = endOfEntity; // continue parsing after the end of the entity
        }
        else
            output.Append(input[i]);
    }
    

    I may have an off-by-one error somewhere in there, but it should be close.

    0 讨论(0)
  • 2020-12-16 16:05

    you can download a local copy of the appropriate HTML and/or XHTML DTDs from the W3C. Then set up an XmlResolver and use it to expand any entities found in the document.

    You could use a regular expression to find/expand the entities, but that won't know anything about context (e.g., anything in a CDATA section shouldn't be expanded).

    0 讨论(0)
  • 2020-12-16 16:10

    would HttpUtility.HtmlDecode work for you?

    I realize it doesn't convert to unicode equivalent entities, but instead converts it to unicode. Is there a specific reason you want the unicode equivalent entities?

    updated edit


            string test = "<p>John &amp; Sarah went to see &ldquo;Scream 4&rdquo;.</p>";
            string decode = HttpUtility.HtmlDecode(test);
            string encode = HttpUtility.HtmlEncode(decode);
    
            StringBuilder builder = new StringBuilder();
            foreach (char c in encode)
            {
                if ((int)c > 127)
                {
                    builder.Append("&#");
                    builder.Append((int)c);
                    builder.Append(";");
                }
                else
                {
                    builder.Append(c);
                }
            }
            string result = builder.ToString();
    
    0 讨论(0)
  • 2020-12-16 16:14

    this might help you put input path in textbox

            try
            {
                FileInfo n = new FileInfo(textBox1.Text);
                string initContent = File.ReadAllText(textBox1.Text);
                int contentLength = initContent.Length;
                Match m;
    
                while ((m = Regex.Match(initContent, "[^a-zA-Z0-9<>/\\s(&#\\d+;)-]")).Value != String.Empty)
                    initContent = initContent.Remove(m.Index, 1).Insert(m.Index, string.Format("&#{0};", (int)m.Value[0]));
    
                File.WriteAllText("outputpath", initContent);
            }
    
            catch (System.Exception excep)
            {
    
                MessageBox.Show(excep.Message);
    
            }
    
    
    
        }
    
    0 讨论(0)
提交回复
热议问题