2014-12-11 3 views
-2

Возможно ли создание формы в HTML из документов XML?Преобразование формы XML в HTML

См., Например, У меня есть следующий XML:

<record> 
    <IsEnrolled>Y</IsEnrolled> 
    <IsGraduating>N</IsGraduating> 
    <StudentLevel>Junior</StudentLevel> 
    <StudentType>InState</StudentType> 
    <HasScholarship>N</HasScholarship> 
    <Action>CodeA</Action> 
    </record> 
    <record> 
    <IsEnrolled>Y</IsEnrolled> 
    <IsGraduating>N</IsGraduating> 
    <StudentLevel>Sophomore</StudentLevel> 
    <StudentType>InState</StudentType> 
    <HasScholarship>Y</HasScholarship> 
    <Action>CodeB</Action> 
    </record> 
    <record> 
    <IsEnrolled>Y</IsEnrolled> 
    <IsGraduating>N</IsGraduating> 
    <StudentLevel>Freshmen</StudentLevel> 
    <StudentType>OutOfState</StudentType> 
    <HasScholarship>Y</HasScholarship> 
    <Action>CodeC</Action> 
    </record> 
    <record> 
    <IsEnrolled>Y</IsEnrolled> 
    <IsGraduating>Y</IsGraduating> 
    <StudentLevel>Senior</StudentLevel> 
    <StudentType>International</StudentType> 
    <HasScholarship>Y</HasScholarship> 
    <Action>CodeD</Action> 
    </record> 

И я хочу, чтобы создать форму HTML из приведенного выше XML:

<body> 
    <table> 
     <tr> 
      <td>IsEnrolled</td>   
      <td> 
       <select> 
        <option>Yes</option> 
        <option>No</option> 
       </select> 
      </td> 
     </tr> 
     <tr> 
      <td>IsGraduating</td>   
      <td> 
       <select> 
        <option>Yes</option> 
        <option>No</option> 
       </select> 
      </td> 
     </tr> 
     <tr> 
      <td>StudentLevel</td> 
      <td> 
       <select> 
        <option>Freshmen</option> 
        <option>Sophomore</option> 
        <option>Junior</option> 
        <option>Senior</option> 
       </select> 
      </td> 
     </tr> 
     <tr> 
      <td>StudentType</td> 
      <td> 
       <select> 
        <option>InState</option> 
        <option>OutOfState</option> 
        <option>International</option> 
       </select> 
      </td> 
     </tr> 
     <tr> 
      <td>HasScholarship</td> 
      <td> 
       <select> 
        <option>Yes</option> 
        <option>No</option> 
       </select> 
      </td> 
     </tr> 
    </table> 
     <span id="Action">Display the Action here based on the selected answers above</span> 
</body> 

Действие элемент в XML является выход на вопросы. Я хочу отобразить этот вывод в элементе на основе того, что выбрано в HTML-форме.

Возможно ли это? Если да, то как мне подойти к этому?

+0

карту XML к сущности графу, а затем вводят его в шаблон представления, Кстати ваш вопрос слишком широк – InferOn

+0

@InvernoMuto вы могли бы указать мне любой ресурс для сопоставления графа сущности? Я понимаю, что это широкий вопрос, но я хотел знать, возможно ли это и как подойти к этому. – user793468

ответ

1

Поскольку у нас нет много деталей, это отправная точка, чтобы получить то, что вам нужно; мы можем представить себе гипотетический индекс действия:

public ActionResult Index() 
     { 

Это ваша XML-строка, если не корневой узел, вы легко можете добавить его на лету

  var xmlInput = @"<?xml version=""1.0"" ?><CustomData><Records><record> 
         <IsEnrolled>Y</IsEnrolled> 
         <IsGraduating>N</IsGraduating> 
         <StudentLevel>Junior</StudentLevel> 
         <StudentType>InState</StudentType> 
         <HasScholarship>N</HasScholarship> 
         <Action>CodeA</Action> 
         </record> 
         <record> 
         <IsEnrolled>Y</IsEnrolled> 
         <IsGraduating>N</IsGraduating> 
         <StudentLevel>Sophomore</StudentLevel> 
         <StudentType>InState</StudentType> 
         <HasScholarship>Y</HasScholarship> 
         <Action>CodeB</Action> 
         </record> 
         <record> 
         <IsEnrolled>Y</IsEnrolled> 
         <IsGraduating>N</IsGraduating> 
         <StudentLevel>Freshmen</StudentLevel> 
         <StudentType>OutOfState</StudentType> 
         <HasScholarship>Y</HasScholarship> 
         <Action>CodeC</Action> 
         </record> 
         <record> 
         <IsEnrolled>Y</IsEnrolled> 
         <IsGraduating>Y</IsGraduating> 
         <StudentLevel>Senior</StudentLevel> 
         <StudentType>International</StudentType> 
         <HasScholarship>Y</HasScholarship> 
         <Action>CodeD</Action> 
         </record></Records></CustomData>"; 

Ниже приводится представление ваши объекты в XML:

[XmlTypeAttribute(AnonymousType = true)] 
     public class CustomData 
     { 
      [XmlArray(ElementName = "Records")] 
      [XmlArrayItem(ElementName = "record")] 
      public List<Record> Records { get; set; } 

      public CustomData() 
      { 
       Records = new List<Record>(); 
      } 

     } 
     public class Record 
     { 
      [XmlElement(ElementName = "IsEnrolled")] 
      public string IsEnrolled { get; set; } 

      [XmlElement(ElementName = "IsGraduating")] 
      public string IsGraduating { get; set; } 

      [XmlElement(ElementName = "StudentLevel")] 
      public string StudentLevel { get; set; } 

      [XmlElement(ElementName = "StudentType")] 
      public string StudentType { get; set; } 

      [XmlElement(ElementName = "HasScholarship")] 
      public string HasScholarship { get; set; } 

      [XmlElement(ElementName = "Action")] 
      public string Action { get; set; } 
     } 

на этом этапе вы просто должны десериализации XML:

  var serializer = new XmlSerializer(typeof(CustomData)); 

      CustomData data; 
      using (TextReader reader = new StringReader(xmlInput)) 
      { 
       data = (CustomData)serializer.Deserialize(reader); 
      } 

... и вернуть его свой вид

  return View(data); 
    } 

... Теперь вы остаетесь только, чтобы сделать вашу модель

Смежные вопросы