Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9251285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:39:29+00:00 2026-06-18T10:39:29+00:00

I am using Razor outside of ASP.NET to generate XML documents from a template.

  • 0

I am using Razor outside of ASP.NET to generate XML documents from a template. I have the following code:

<event>
    <timeouts trigger='@Model.TriggerTimeout' />
    <parameters>
    @foreach (var parameter in Model.Parameters) {
        <@parameter.Key>@parameter.Value</@parameter.Key>
    }
    </parameters>
</event>

where Model.Parameters is just a name/value collection. My expected output should be:

<event>
    <timeouts trigger='100' />
    <parameters>
        <name1>value1</name1>
        <name2>value2</name2>
        <name3>value3</name3>
    </parameters>
</event>

but what I am actually getting is:

<event>
    <timeouts trigger='100' />
    <parameters>
        <name1>value1</@parameter.Key>
        <name2>value2</@parameter.Key>
        <name3>value3</@parameter.Key>
    </parameters>
</event>

What am I doing wrong? What is the secret sauce that I need to apply to make the dynamic elements close properly?

EDIT
So now my model evolves to have multiple events, and my Razor code looks like this:

<events>
@foreach (var _event in Model.Events) {
    <@_event.Name enabled='@_event.Enabled'>
        <timeouts trigger='@_event.TriggerTimeout' />
        <parameters>
        @foreach (var parameter in _event.Parameters) {
            @:<@parameter.Key>@parameter.Value</@(parameter.Key)>
        }
        </parameters>
    </@_event.Name>
}
</events>

the output should be:

<events>
    <event1 enabled='true'>
        <timeouts trigger='100' />
        <parameters>
            <name1>value1</name1>
            <name2>value2</name2>
        </parameters>
    </event1>
    <event2 enabled='false'>
        <timeouts trigger='100' />
        <parameters>
            <name1>value1</name1>
        </parameters>
    </event2>
</events>

but it actually is:

<events>
    <event1 enabled='true'>
        <timeouts trigger='100' />
        <parameters>
            <name1>value1</name1>
            <name2>value2</name2>
        </parameters>
    </@_event.Name>
    <event2 enabled='false'>
        <timeouts trigger='100' />
        <parameters>
            <name1>value1</name1>
        </parameters>
    </@_event.Name>
</events>

SOLUTION
Through a lot of trial and error, I eventually tracked down the missing secret sauce. The opening dynamic XML element needed a ?: prefix to render the closing element correctly. Most weird.

<events>
@foreach (var _event in Model.Events) {
    @:<@_event.Name enabled='@_event.Enabled'>
        <timeouts trigger='@_event.TriggerTimeout' />
        <parameters>
        @foreach (var parameter in _event.Parameters) {
            @:<@parameter.Key>@parameter.Value</@(parameter.Key)>
        }
        </parameters>
    </@_event.Name>
}
</events>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:39:30+00:00Added an answer on June 18, 2026 at 10:39 am

    You need to write </@(parameter.Key)> to stop Razor from thinking that that’s an email address.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Asp.Net mvc 3 project I'm using razor, and need to generate
I have code like below in ASP.Net MVC4. and I am using Razor engine.
I am using Razor syntax in an ASP.NET MVC3 View inside a JavaScript code
I am developing an ASP.Net MVC 3 Web application using Razor Views. I have
I'm using Razor in MVC 3 and Asp.net C#. I have a View with
I have an MVC4 (using razor) main site and then an asp.net webforms store
I'm trying to generate fairly simple xml using razor template engine. This is my
I have a Asp.net MVC application(using Razor View) in which i am using Html.Textboxfor
I'm new to using Razor and ASP.NET. I currently have a site I'm trying
In my ASP.net MVC App (using Razor views) I have a ProductDetails view. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.