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);

Generating XSD from and XML file

If you want to generate an XML schema (*.xsd) file from an existing XML file you can use the xsd.exe tool.

Go to your Visual Studio command line and type in the following:

xsd.exe C:\file.xml /outputdir:C:\

Done!

Friday 27 March 2009

Navigating XElement

The XMl looks like this:




I first create a navigator with an XPath expression to get the nodes set I want to work with:

XElement root = XElement.Load("data.xml");
var answers = root.CreateNavigator().Select("/Category/Subject/Question/Answer");

Then I loop through each answer...

while (answers.MoveNext())
{

Here I convert the underlying object to an Element to get the values:

var details = answers.Current.UnderlyingObject as XElement;

Now I can access the elements I want:

var val = details.Element("Description").Value;

}

ViewState Helper

A great tool to aid in viewing the percentage of the page that is ViewState.

ASP.NET ViewState Helper

Thursday 26 March 2009

BareTail

BareTail is a very useful log file viewer. All you need to do is point it to the latest log file i.e.

C:\WINDOWS\system32\Logfiles\W3SVC1\ex080512.log

You can download it from: BareTail