📜  使用 c# 代码示例在 xml 中使用 & symbole 作为文本

📅  最后修改于: 2022-03-11 14:49:23.079000             🧑  作者: Mango

代码示例1
public static string EscapeXml( this string s )
{
  string toxml = s;
  if ( !string.IsNullOrEmpty( toxml ) )
  {
    // replace literal values with entities
    toxml = toxml.Replace( "&", "&" );
    toxml = toxml.Replace( "'", "'" );
    toxml = toxml.Replace( "\"", """ );
    toxml = toxml.Replace( ">", ">" );
    toxml = toxml.Replace( "<", "<" );
  }
  return toxml;
}

public static string UnescapeXml( this string s )
{
  string unxml = s;
  if ( !string.IsNullOrEmpty( unxml ) )
  {
    // replace entities with literal values
    unxml = unxml.Replace( "'", "'" );
    unxml = unxml.Replace( """, "\"" );
    unxml = unxml.Replace( ">", ">" );
    unxml = unxml.Replace( "<", "<" );
    unxml = unxml.Replace( "&", "&" );
  }
  return unxml;
}