I have an existing .net application rendering data in razor, and I would like to display a specific node from an XML string for each record using razor. But I am not sure how to go about writing it. There will be 50+ records in the model, so I want to make sure it's efficient.

Each record in the model will have it's own xmldata. For example:


Model:

namespace Project.Web.ViewModels
{  
   public class SampleModel  
   {  
      public int id { get; set; }  
      public string xmldata { get; set; }  
   }  
}


xmldata for first record:

<content>
  <user>
    <firstname>Jim</firstname>
    <lastname>Smith</lastname>
  </user>
</content>


xmldata for second record:

<content>
  <user>
    <firstname>John</firstname>
    <lastname>Jones</lastname>
  </user>
</content>


csHtml:

@model Project.Web.ViewModels.SampleModel
@foreach (item in Model) {
   <p><b>ID:</b> @item.id</p>
   <p><b>FirstName:</b> //Add firstname from @item.xmldata here</p>
   <p><b>LastName:</b> //Add lastname from @item.xmldata here</p>
   <br>
}


Desired output:

ID: 1
FirstName: Jim
LastName: Smith

ID: 2
FirstName: John
LastName: Jones


An example of how to write the razor code in cshtml would be greatly appreciated!

Related posts

Recent Viewed