如何在Razor视图中显示xml元素?

时间:2013-03-14 15:17:40

标签: xml asp.net-mvc razor

如何在Razor视图中硬编码和显示xml元素。

我想显示以下元素,例如它们在元素中:

<profile>
    <status>Active</status> 
    <userIdentifier>24d05ce9-7d11-4f06-a9eb-9f33ceac7f91</userIdentifier> 
    <firstName>dd</firstName>
    <lastName>dasd</lastName>
    <title>Ms</title>
    <emailAddress>test@test.co.uk</emailAddress>
    <companyAddress>
        <companyName></companyName>
        <addressLine1></addressLine1>
        <addressLine2></addressLine2>
        <addressLine3>Sutton</addressLine3>
        <addressLine4>Surrey</addressLine4>
        <town>Sutton</town>
        <stateOrCounty>Surrey</stateOrCounty>
        <postOrZipCode>SM1 5AS</postOrZipCode>
        <country>United Kingdom</country>
        <region/>
    </companyAddress>
    <contactAddress>        
        <addressLine1>30 Park Avenue South</addressLine1>
        <addressLine2>New York </addressLine2>
        <addressLine3>NY 11010</addressLine3>
        <addressLine4>USA</addressLine4>
        <town>NY</town>
        <stateOrCounty>NY</stateOrCounty>
        <postOrZipCode>11010</postOrZipCode>
        <country>United States</country>
        <region></region>
    </contactAddress>
    <demographics>
        <demographic key="ESOATDropDownList1">
            <answer>True</answer>
        </demographic>
    </demographics>
</profile>

怎么可能呢?

我知道它可以写在控件中。有没有更好/更容易的方法?

2 个答案:

答案 0 :(得分:6)

你可以试试这个:

@{
   var profileXml = @"
<profile>
    <!-- your xml -->
</profile>";
}
<pre>
    @Html.Raw(Html.Encode(profileXml))
</pre>

此处profileXml包含XML字符串。

答案 1 :(得分:0)

如果控制器中有XML,则可以使用return xml内容:

public ActionResult XmlContent()
{
    string xml = @"<profile>
                     <!-- your xml -->
                   </profile>";
    return Content(xml, "text/xml");            
}