HTML and CSS Reference
In-Depth Information
string fileName = file.FileName;
fileName = Server.MapPath("~/Content/Uploads/" + fileName);
file.SaveAs(fileName);
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(“”, Server.MapPath(“~/Content/Employees.xsd”));
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += OnValidationError;
XmlReader reader = XmlReader.Create(fileName, settings);
while (reader.Read())
{
}
reader.Close();
}
}
Response.ContentType = "text/plain";
StringBuilder sb = new StringBuilder();
sb.Append("<ul>");
foreach (string error in errors)
{
sb.Append("<li>" + error + "</li>");
}
sb.Append("</ul>");
return Json(sb.ToString());
}
void OnValidationError(object sender, ValidationEventArgs e)
{
string fileName = Path.GetFileName(((XmlReader)sender).BaseURI);
errors.Add(fileName + " encountered an error - " + e.Exception.Message);
}
The code from Listing 9-31 that saves the uploaded XML files is identical to the previous example. The
code marked in bold is responsible for validating the XML files against an XSD schema file: Employees.xsd .
The Employees.xsd schema file expects the XML markup in the format shown in Listing 9-32.
Listing 9-32. Sample XML File
<?xml version="1.0" encoding="utf-8" ?>
<employees>
<employee employeeid="1">
<irstname>Nancy</irstname>
<lastname>Davolio</lastname>
<homephone>(206) 555-9857</homephone>
<notes>
<![CDATA[...]]>
</notes>
</employee>
Although this example uses a fixed schema file, you can pick a schema file based on a condition. The
XmlReader class is used to read XML documents. The XmlReaderSettings class attaches Employees.xsd to
the XmlReader . When you call the Read() method of the XmlReader class, the XML document is read and
 
Search WWH ::




Custom Search