Monday 30 March 2009

Validate XML with XSD

In order to make sure your XML matches your XSD schema you need to link it. I have done this below using XElement (LINQ to XML) and then saved the file:

XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";

var userSettings = new XElement("UserSettings",
new XAttribute(XNamespace.Xmlns + "xsi", ns.NamespaceName),
new XAttribute(ns + "noNamespaceSchemaLocation", ConfigurationManager.AppSettings.Get("ChartSchemaLocation")),
new XElement("Options",
new XElement("ChartType", ChartTypes.SelectedValue),
new XElement("DrawingStyle", DrawingStyle.SelectedValue),
new XElement("Theme", Theme.SelectedValue),
new XElement("Show3D", Show3D.Checked.ToString()),
new XElement("Weighted", Weighted.Checked.ToString()),
new XElement("Vertical", Vertical.Checked.ToString()),
new XElement("DisplayTitleAxis", ShowAxisTitles.Checked.ToString())));

userSettings.Save(UserSettingsFilePath);

No comments: