简体   繁体   中英

Meta tag from c# linq to xml display <

    XmlDocument doc = new XmlDocument();
    XDocument XMLDoc = XDocument.Load(Path.Combine(Request.PhysicalApplicationPath, "App_Data/google_meta2.xml"));
    string id = "2"; // id to be selected

    XElement Contact = (from xml2 in XMLDoc.Descendants("Keywords")
                        where xml2.Element("ID").Value == id
                        select xml2).FirstOrDefault();

    string key = HttpUtility.HtmlDecode(Contact.Element("name").ToString()); 

     /* not works :
    string cleanedString = key ;

    cleanedString = cleanedString.Replace("<","&lt;");      // No code
    cleanedString = cleanedString.Replace(">", "&gt;");
    cleanedString = cleanedString.Replace("&", "&amp;");    // No query string breaks
     */

    HtmlMeta meta = new HtmlMeta();
    meta.Name = "keywords";
    meta.Content = key;
    MetaPlaceHolder.Controls.Add(meta);

XML

<?xml version="1.0" encoding="utf-8"?>
<Google>
  <Keywords>
    <ID>2</ID>
      <name>bla</name>
  </Keywords>
</Google>

Output

<meta name="keywords" content="&lt;name>bla&lt;/name>" /></head>

How can i remove the &lt, or there is better way to do that ?

Try retrieving the attribute from the XElement instead of using HtmlDecode:

XElement contact = (from xml2 in XMLDoc.Descendants("Keywords")
                    where xml2.Element("ID").Value == id
                    select xml2).FirstOrDefault();

HtmlMeta meta = new HtmlMeta();
meta.Name = "keywords";
meta.Content = contact.Attribute("name").Value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM