Displaying / editing xml in an MVC app

随声附和 提交于 2019-12-21 20:34:33

问题


I need to maintain an xml column in a SQL database.

a) in my details page - I just want to display this column as "pretty" xml - like the browser would. So instead of one big block of xml (e.g. <foo><bar><data>one</data><data>two</data><data>three</data></bar><other>something else></other></foo> ) I want it split out with each new tag on a new line (e.g.

<foo>
  <bar>
    <data>one</data>
    <data>two</data>
    <data>three</data>
  </bar>
  <other>something else></other>
</foo>

and

b) in my edit page I want a simple form with the tags as headings (in bold for instance) and the node values as editable text boxes - but I want this to be created dynamically from the xml itself. I don't want to hard-code the tag labels in my code, but loop through the xml and fetch them on the fly.

I'm using c# .net for my MVC application with Sql Server as the database and LINQ to SQL.


回答1:


For your first question you can put the XML string in a XDocument.

XDocument doc = XDocument.Parse("<foo><bar><data>one</data><data>two</data><data>three</data></bar><other>something else></other></foo>");

ViewData["XmlData"] = doc.ToString();

Now in your view present the value of ViewData["XmlData"] in a <pre><code> HTML element.



来源:https://stackoverflow.com/questions/1426048/displaying-editing-xml-in-an-mvc-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!