English 中文(简体)
• 如何用C# 标注a至2225? (UTF8)
原标题:How to encode á to &#225 with C# ?? (UTF8)

我试图与UTF-8号编码书写XML文件,而原始拼法则可能具有诸如黑白等无效特性,因此,需要将这些无效性质改变为有效性质。

我知道,有一种编码方法,例如,特征a,将其改为特性类别á

我正试图用C#but,即没有结果。 我正在使用编码。 UTF8功能,但仅以ema性质(即:a)或某种特性结束? 性质。

因此,你们是否知道,用C#来消除这种性质变化的正确方式?

感谢你们的时间和帮助:

当地雇员

最佳回答

甲并非“无效”性质。 该公司有UTF-8编码(195和161条),Nick是正确的,如果你正确构造一切,就会透明。

问题回答

你可以使用任何一种方法。

下面,在C#中,你可以采用四种方式将XML编码为:

  1. string.Replace() 5 times

很显然,它运作良好。 注:Replace(&”、“&>>>必须首先替换,以便我们不取代其他已经越狱的人。

string xml = "<node>it s my "node" & i like it<node>";
encodedXml = xml.Replace("&","&amp;").Replace("<","&lt;").Replace(">","&gt;").Replace(""", "&quot;").Replace(" ", "&apos;");

// RESULT: &lt;node&gt;it&apos;s my &quot;node&quot; &amp; i like it&lt;node&gt;
  1. System.Web.HttpUtility.HtmlEncode()

用于编码超文本,但超文本是XML的一种形式,这样我们也可以使用。 大部分用于伙伴关系。 NET指。 注:HtmlEncode do NOT encode ́trophes( )。

string xml = "<node>it s my "node" & i like it<node>";
string encodedXml = HttpUtility.HtmlEncode(xml);

// RESULT: &lt;node&gt;it s my &quot;node&quot; &amp; i like it&lt;node&gt;
  1. System.Security.SecurityElement.Escape()

在Windows表格或Console,我使用这种方法。 如果没有其他东西,它就拯救了我,包括该制度。 我的项目中的网络参考资料,并包含所有5个术语。

string xml = "<node>it s my "node" & i like it<node>";
string encodedXml = System.Security.SecurityElement.Escape(xml);

// RESULT: &lt;node&gt;it&apos;s my &quot;node&quot; &amp; i like it&lt;node&gt;
  1. System.Xml.XmlTextWriter

利用XmlTextWriter,你不必担心,因为必要时会越出果园。 例如,从属性来看,它只是从外逃,而在代谢价值中,它就没有逃避外逃。

string xml = "<node>it s my "node" & i like it<node>";
using (XmlTextWriter xtw = new XmlTextWriter(@"c:xmlTest.xml", Encoding.Unicode))
{

    xtw.WriteStartElement("xmlEncodeTest");
    xtw.WriteAttributeString("testAttribute", xml);
    xtw.WriteString(xml);
    xtw.WriteEndElement();

}

// RESULT:
/*
<xmlEncodeTest testAttribute="&lt;node&gt;it s my &quot;node&quot; &amp; i like it&lt;node&gt;">
    &lt;node&gt;it s my "node" &amp; i like it&lt;node&gt;
</xmlEncodeTest>
*/

[http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Differ-ways-how-to-einski-an-XML-string-in-C.aspx]

    private static string Escape(string content)
    {
        var sb = new StringBuilder();
        var settings = new XmlWriterSettings 
        { 
            ConformanceLevel = ConformanceLevel.Fragment 
        };

        using (var xmlWriter = XmlWriter.Create(sb, settings))
            xmlWriter.WriteString(content);

        return sb.ToString();
    }

This is exactly what you need: (found at http://www.codeproject.com/Articles/20255/Full-HTML-Character-Encoding-in-C)

//for example this transforms "čas" to "&#269;as"
public static string HtmlEncode(string text)
    {
        char[] chars = HttpUtility.HtmlEncode(text).ToCharArray();
        StringBuilder result = new StringBuilder(text.Length + (int)(text.Length * 0.1));

        foreach (char c in chars)
        {
            int value = Convert.ToInt32(c);
            if (value > 127)
                result.AppendFormat("&#{0};", value);
            else
                result.Append(c);
        }

        return result.ToString();
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签