Querying XML with XPathDocument and XPathNodeIterator (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Untitled Page
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Xml.XPath;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string booksFile = Path.Combine(Request.PhysicalApplicationPath, "Data.xml");
XPathDocument document = new XPathDocument(booksFile);
XPathNavigator nav = document.CreateNavigator();
XmlNamespaceManager namespaceMgr = new XmlNamespaceManager(nav.NameTable);
namespaceMgr.AddNamespace("b", "http://example.books.com");
foreach (XPathNavigator node in nav.Select("//b:book[not(b:price[. > 10.00])]/b:price",namespaceMgr))
{
Decimal price = (decimal)node.ValueAs(typeof(decimal));
Response.Write(String.Format("Price is {0}
", price));
}
}
}
File: Data.xml
title 1
A
B
8
title 2
C
D
11.99