title: How to validate XML file against DTD and XML schema in ASP.NET MVC4?
body: <p>I am creating XML file from SQL SERVER database. This saves XML file into computer:</p>
  
  <pre><code>List&lt;Product&gt; prList = db.Product.ToList();
          if (prList.Count &gt; 0)
          {
              var xEle = new XElement("Products",
                  from product in prList
                  select new XElement("Product",
                      new XElement("ProductId", product.Id),
                      new XElement("Name", product.Name),
                      new XElement("Desctiption", product.Description),
                      new XElement("Price", product.Price),
                      new XElement("PictureURL", product.PictureURL),
                      new XElement("Category", product.Category.CategoryName)
                      ));
              HttpContext context = System.Web.HttpContext.Current;
              context.Response.Write(xEle);
              context.Response.ContentType = "application/xml";
              context.Response.AppendHeader("Content-Disposition", "attachment; filename=Products.xml");
              context.Response.End();
  
              return View();
          }
          return View();
  </code></pre>
  
  <p>I have DTD:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!ELEMENT Products ((Product+))&gt;
  &lt;!ELEMENT ProductId (#PCDATA)&gt;
  &lt;!ELEMENT Product ((ProductId, Name, Desctiption, Price, PictureURL, Category))&gt;
  &lt;!ELEMENT Price (#PCDATA)&gt;
  &lt;!ELEMENT PictureURL (#PCDATA)&gt;
  &lt;!ELEMENT Name (#PCDATA)&gt;
  &lt;!ELEMENT Desctiption (#PCDATA)&gt;
  &lt;!ELEMENT Category (#PCDATA)&gt;
  </code></pre>
  
  <p>and XML schema:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;xs:element name="Products"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element ref="Product" maxOccurs="unbounded"/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      &lt;xs:element name="ProductId"&gt;
          &lt;xs:simpleType&gt;
              &lt;xs:restriction base="xs:byte"&gt;
                  &lt;xs:enumeration value="1"/&gt;
                  &lt;xs:enumeration value="2"/&gt;
                  &lt;xs:enumeration value="3"/&gt;
                  &lt;xs:enumeration value="4"/&gt;
                  &lt;xs:enumeration value="5"/&gt;
              &lt;/xs:restriction&gt;
          &lt;/xs:simpleType&gt;
      &lt;/xs:element&gt;
      &lt;xs:element name="Product"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element ref="ProductId"/&gt;
                  &lt;xs:element ref="Name"/&gt;
                  &lt;xs:element ref="Desctiption"/&gt;
                  &lt;xs:element ref="Price"/&gt;
                  &lt;xs:element ref="PictureURL"/&gt;
                  &lt;xs:element ref="Category"/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      &lt;xs:element name="Price"&gt;
          &lt;xs:simpleType&gt;
              &lt;xs:restriction base="xs:decimal"&gt;
                  &lt;xs:enumeration value="12400.0"/&gt;
                  &lt;xs:enumeration value="15.0"/&gt;
                  &lt;xs:enumeration value="29.0"/&gt;
                  &lt;xs:enumeration value="388.0"/&gt;
                  &lt;xs:enumeration value="69.0"/&gt;
              &lt;/xs:restriction&gt;
          &lt;/xs:simpleType&gt;
      &lt;/xs:element&gt;
      &lt;xs:element name="PictureURL" type="xs:string"/&gt;
      &lt;xs:element name="Name" type="xs:string"/&gt;
      &lt;xs:element name="Desctiption" type="xs:string"/&gt;
      &lt;xs:element name="Category"&gt;
          &lt;xs:simpleType&gt;
              &lt;xs:restriction base="xs:string"&gt;
                  &lt;xs:enumeration value="Beauty"/&gt;
                  &lt;xs:enumeration value="Electronics"/&gt;
                  &lt;xs:enumeration value="Office"/&gt;
                  &lt;xs:enumeration value="Sports"/&gt;
                  &lt;xs:enumeration value="Watches"/&gt;
              &lt;/xs:restriction&gt;
          &lt;/xs:simpleType&gt;
      &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>My problem is I do not know how to validate my creating XML against DTD and XML schema in runtime. Please, can you give your suggestions how to implement this? What is the easiest way?</p>
  
  <p>I have database which includes Products table. Products table's attributes: Id, Name, Description, Price, PictureURL, Category(FK to Category table's Id). I am taking all products in the form of list. Then saving into XML file. My CW task requires me to validate against DTD and XML schema.</p>