Tuesday, 19 May 2009

Linq to XMl Joins

Here is an example of a left join:

var joinXml = from m in result.Descendants("Report")
join o in ordered.Descendants("Report")
on (string)m.Attribute("name").Value.ToLower()
equals (string)o.Attribute("name").Value.ToLower()
into mainGroup
let orderedMatch = (
from x in mainGroup.DefaultIfEmpty()
where x != null
select new XElement("Report",
new XAttribute("name", m.Attribute("name").Value),
new XAttribute("trend", "true"))
)
let orderedNoMatch = (
from y in mainGroup.DefaultIfEmpty()
where y == null
select new XElement("Report",
new XAttribute("name", m.Attribute("name").Value),
new XAttribute("trend", "false"))
)
let total = orderedMatch.Concat(orderedNoMatch)
select total;

Wednesday, 13 May 2009

LINQ to XML

Below is an example of how I used LINQ to XML to read an XML string and search for nodes that match a criteria within the tree. It also shows how I order by an attribute.

The XML:



The Code:


Thursday, 23 April 2009

How to find controls in ListView ItemTemplate

This example below explains how to find controls and set them in the ItemTemplate of a ListView control when using XmlDataSource:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (ListView1.EditItem == null)
{
// Format the vertical % to 2 decimal places.
var data = (ListViewDataItem)e.Item;
var vert = XPathBinder.Eval(data.DataItem, "./Vert", "0");
var formatVert = float.Parse(vert.Replace("%", ""));

((Literal)e.Item.FindControl("Literal8")).Text = formatVert.ToString(VERT_FORMAT);
}
}

Thursday, 16 April 2009

App_Code Class not working

I have a class in my App_Code folder and I am trying to reference it in my code behind. The name spaces are the same but I keep getting an error that is can't find the class.

The reason being I am using a web application project and not a web site project. In order for the class to be recognised I need to change the "Build Action" of the class file to "Complie" from "Content".

Build and it works now.

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;

}