简体   繁体   中英

Force Closing Html Tags in Asp.Net

Im working on a asp.net page at the moment and I have a string with some news in the database. Im showing a short version of the newest news on my main page. Im using the substring method to cut the rest of the news after 25 chars. Its working pretty good. The Problem is when the 'news' has list tags like:

<p> Hello all! </p> 
<ul>
  <li> Telling you something:
    <ul>
      <li> Telling you that...
    </ul>
  </li>
  <li> Telling you something else
  <li>
...

IE v.9 is not cutting the part after 25 chars correctly - it showes one more list dot after 25 chars. On Firefox and Chrome its working... but not with the IE v9. I think the problems are the open html tags and the auto completion of IE9. Is there any option to force close all tags (from the code behind) or to avoid the auto completion on IE9?

Maybe someone else has a better idea to fix the problem.

Would it be possible for you to load your HTML string into an XmlDocument on the server side before returning the data to the client?

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml("{your-html-string}");

If so, you could manipulate your XmlDocument by removing unwanted nodes, as shown here: Removing nodes from an XmlDocument . To do this, you could loop through your source XmlDocument and only copy those nodes into your target XmlDocument that fit your summarizing criterion (length <= 25, for example).

Once you had your target XmlDocument finalized, you could output your abbreviated XmlDocument as a string (à la Convert XmlDocument to String ).

This would give you what you needed but without having to damage your Html by forcing truncation at a specific length.

If your HTML is not XML compatible (not strictly XHTML), then you could try using the Html Agility Pack to parse your source HTML.

Its not possible to save the html string in a xml file on the server. I load the news string from the DB directly. Is there any other option?

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