我试图在MVC3视图中显示缩进的XML,我不能让它工作,它只是显示非缩进?

EdB*_*EdB 0 asp.net-mvc asp.net-mvc-3 asp.net-mvc-2

正如John Saunders所建议的那样,我一直在尝试使用XElement.但是我的XML没有在Razor View中缩进.我一定是在做傻事,但我看不到.

控制器代码:

XElement myXElement = XElement.Load(strMapPath + strFileName);
ViewBag.MyOrigDocXML = myXElement;
return View();
Run Code Online (Sandbox Code Playgroud)

Razor Code in View:

@if(ViewBag.MyOrigDocXML != null)
{
@ViewBag.MyOrigDocXML.ToString();
}
Run Code Online (Sandbox Code Playgroud)

任何帮助非常感谢,

Iai*_*ain 7

我会将你的代码嵌套在pre html标签中

<link href="~/Content/Prettify/prettify.css" rel="stylesheet" />
<script src="~/Scripts/Prettify/prettify.js"></script>

<body onload="prettyPrint()">
    <pre class="prettyprint lang-xml">@ViewBag.MyOrigDocXML;</pre>
</body>
Run Code Online (Sandbox Code Playgroud)

这将以缩进的布局为您提供xml,然后添加美化以突出显示xml文本

  • 在过去,我使用了Google Prettify(来自Nuget - Install-Package Prettify)来突出显示文本.我更新了答案,在示例中包含Prettify. (2认同)