Replace elements in html template with property values ​​of C # object

1: I have an .html file with some markup with some placeholder tags.

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> First Name : <FIRSTNAME/> <br /> Last Name: <LASTNAME/> </body> </html> 

2: I have a class for storing data returned from db

 public class Person { public Person() { } public string FirstName { get; set; } public string LastName { get; set; } } 

3: PersonInfo.aspx I write this .html with a replaceable placeholder with actual values.

 public partial class PersonInfo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Person per= new Person(); per.FirstName = "Hello"; per.LastName = "World"; string temp = File.ReadAllText(Server.MapPath("~/template.htm")); temp = temp.Replace("<FIRSTNAME/>", per.FirstName); temp = temp.Replace("<LASTNAME/>", per.LastName); Response.Write(temp); } } 

4: PersonInfo.aspx is literally empty since I am inserting html from the code.

When PersonInfo.aspx is called, the layout of the html template will be displayed with the corresponding values ​​in the placeholder. It is also likely that I want to send the final markup to the html email address (although this is not part of the question since I know how to send email).

Is this the best way to populate the values ​​in my html template or some other better alternative?

Note. This is a very simple example. My class is very complex with objects as properties, and also my html template contains 40-50 placeholders.

So the code in my step 3 will require 40-50 Replace statements.

Feel free to ask if there are any doubts and whether you really appreciate any inputs.

+4
source share
4 answers

I would modify your page to use xslt. This is a really easy way to do replacement stuff from a database. The only work you will need to do is make your XML document with data from your database.

+1
source

If your page is valid XML (as I assume it is based on an example), you can parse it in XElement and search for children by name node. You can probably write a more efficient version, but an example would be:

.NET 3.5

  public void Page_Load(object sender, EventArgs e) { Person person = new Person { FirstName = "Hello", LastName = "World" }; var dictionary = typeof(Person).GetProperties(BindingFlags.Public | BindingFlags.Instance) .ToDictionary(p => p.Name.ToUpperInvariant(), p => (p.GetValue(person, null) ?? string.Empty).ToString()); var xe = XElement.Parse(File.ReadAllText(HttpContext.Current.Server.MapPath("~/template.htm"))); foreach (var key in dictionary.Keys) { foreach (var match in xe.Descendants(key)) { match.ReplaceAll(dictionary[key]); } } } 

.NET 2.0 Friendly:

  var person = new Person(); person.FirstName = "Hello"; person.LastName = "World"; var dictionary = new Dictionary<string, string>(); foreach(var propertyInfo in typeof(Person).GetProperties(BindingFlags.Public|BindingFlags.Instance)) { var elementName = propertyInfo.Name.ToUpperInvariant(); var value = (propertyInfo.GetValue(person, null) ?? string.Empty).ToString(); dictionary.Add(elementName,value); } var xml = new XmlDocument(); xml.LoadXml(File.ReadAllText(HttpContext.Current.Server.MapPath("~/template.htm"))); foreach(var key in dictionary.Keys) { var matches = xml.GetElementsByTagName(key); for(int i = 0; i<matches.Count;i++) { var match = matches[i]; var textNode = xml.CreateTextNode(dictionary[key]); match.ParentNode.ReplaceChild(textNode, match); } } 
+3
source

I would use the SqlDataSource and Repeater control. They make it easy to retrieve data from a database and then display it on a page. You would not need to do the code, as everything will be processed for you by .NET.

Your data source will look something like this:

 <asp:sqlDataSource ID="mySqlDataSource" SelectCommand="SELECT firstName, lastName FROM tablename" /> 

And then you need a control that can use the returned data and display it on the page:

 <asp:Repeater runat="server"> <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <tr> <td>First Name: <%#Container.DataItem("firstName")%>"</td> </tr> <tr> <td>Last Name: <%#Container.DataItem("lastName")%></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> 

Edit:

Since you do not want to use the data source and the database binding control, I would recommend the XmlTextWriter class. There is a very good tutorial here . This will allow you to be more flexible than if you were reading a template from a separate HTML file.

+1
source

As you are generating an email, consider using MailDefinition.CreateMailMessage . You will have to copy the placeholders and substitution values ​​into the Dictionary object.

+1
source

Source: https://habr.com/ru/post/1342529/


All Articles