Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符实体转换为其unicode等价物_C#_.net_Html_Character Encoding - Fatal编程技术网

C# 将字符实体转换为其unicode等价物

C# 将字符实体转换为其unicode等价物,c#,.net,html,character-encoding,C#,.net,Html,Character Encoding,我在数据库中有html编码的字符串,但许多字符实体不仅仅是标准的&和。像”这样的实体和&mdash。不幸的是,我们需要将这些数据输入到基于flash的rss阅读器中,flash不会读取这些实体,但它们会读取unicode等价物(例如&) 使用.NET4.0,是否有任何实用方法可以将html编码的字符串转换为使用unicode编码的字符实体 这是一个更好的例子,我需要什么。db有如下html字符串:John&;莎拉去看了$ldquo;尖叫4美元 我需要在rss/xml文档中输出标记中的

我在数据库中有html编码的字符串,但许多字符实体不仅仅是标准的
&
。像
”这样的实体
&mdash。不幸的是,我们需要将这些数据输入到基于flash的rss阅读器中,flash不会读取这些实体,但它们会读取unicode等价物(例如
&

使用.NET4.0,是否有任何实用方法可以将html编码的字符串转换为使用unicode编码的字符实体

这是一个更好的例子,我需要什么。db有如下html字符串:
John&;莎拉去看了$ldquo;尖叫4美元

我需要在rss/xml文档中输出
标记中的内容是:
pJohn& 莎拉去看电影了#8220;尖叫4”./p

我使用XmlTextWriter从数据库记录创建xml文档,类似于下面的示例代码

因此,我需要将数据库中html字符串中的所有字符实体替换为它们的unicode等价物,因为基于flash的rss阅读器无法识别除最常见的实体之外的任何实体,如
&

对你有用吗

我意识到它不会转换为unicode等效实体,而是转换为unicode。您需要unicode等效实体的具体原因是什么

更新编辑


string test=“John&;Sarah去看“尖叫4”

”; 字符串解码=HttpUtility.HtmlDecode(测试); 字符串编码=HttpUtility.HtmlEncode(解码); StringBuilder=新的StringBuilder(); foreach(编码中的字符c) { 如果((int)c>127) { builder.Append(&#“); builder.Append((int)c); 生成器。追加(“;”); } 其他的 { 附加(c); } } 字符串结果=builder.ToString();
我的第一个想法是,你的RSS阅读器能接受实际字符吗?如果是这样的话,您可以直接使用和输入它

如果确实需要将其转换为数字表示形式,可以解析出每个实体,
HtmlDecode
it,然后将其转换为
int
,以获得base-10 unicode值。然后将其重新插入字符串中

编辑: 这里有一些代码来演示我的意思(它未经测试,但能让人理解):

string input=“带有—或其他字符实体的内容。”;
StringBuilder输出=新的StringBuilder(input.Length);
for(int i=0;i

我可能在那里的某个地方出现了一个off-by-one错误,但应该很接近。

您可以从W3C下载相应HTML和/或XHTML DTD的本地副本。然后设置一个XmlResolver并使用它展开文档中找到的任何实体


您可以使用正则表达式来查找/扩展实体,但它对上下文一无所知(例如,CDATA部分中的任何内容都不应展开)。

这可能有助于您将输入路径放入文本框中

        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);

        }



    }
试试看
{
FileInfo n=新的FileInfo(textBox1.Text);
string initContent=File.ReadAllText(textBox1.Text);
int contentLength=initContent.Length;
匹配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]);
writealText(“outputpath”,initContent);
}
捕获(系统异常例外)
{
MessageBox.Show(消息除外);
}
}

您确定首先需要角色实体吗?为什么不直接使用实际的unicode字符呢?您可能应该编辑答案,而不是在注释中添加注释。HTMLDE代码不起作用。这仍然需要一个正确编码字符的有效rss提要,但我们的客户还希望将此rss提要用于基于flash的广告网站。正如我在问题中所说,flash不会读取这些不太常见的字符实体,但会读取基于unicode编码的等效字符。不幸的是,这不起作用,因为它将所有非字母数字字符编码为字符实体,包括所有html标记(尖括号等)。我想我应该提供一个更好的例子(见上文)。谢谢你的尝试,我会继续尝试你的代码,看看我是否能让它工作。谢谢你,我相信它会工作的。您使用HTMLDE代码将其输入flash rss阅读器是正确的,但这些代码由我们的客户控制,我认为他们并不真正知道自己在做什么。@Dan听起来像一个典型的客户:)这里有另一个解决方案,使用我在Xamarin中使用的正则表达式。它在单个文件中替换,但我想在多个文件中替换。有人能帮我吗
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]);
}
        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);

        }



    }