Csharp/CSharp Tutorial/XML/XmlPathNavigator — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:17, 26 мая 2010
Содержание
<source lang="csharp">using System; using System.Xml.XPath; using System.Xml; class MainClass {
static void Main(string[] args) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"c:\books.xml"); XPathNavigator nav = xmlDoc.CreateNavigator(); Console.WriteLine("Author First Name"); XPathNodeIterator itrator = nav.Select("descendant::first-name"); while( itrator.MoveNext() ) { Console.WriteLine(itrator.Current.Value.ToString()); } }
}</source>
<source lang="csharp">using System; using System.Xml.XPath; using System.Xml; class MainClass {
static void Main(string[] args) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"c:\books.xml"); XPathNavigator nav = xmlDoc.CreateNavigator(); nav.MoveToRoot(); string name = nav.Name; Console.WriteLine("Root node info: "); Console.WriteLine("Base URI" + nav.BaseURI.ToString()); Console.WriteLine("Name: " +nav.Name.ToString()); Console.WriteLine("Node Type: "+ nav.NodeType.ToString()); Console.WriteLine("Node Value: "+nav.Value.ToString()); if (nav.HasChildren) { nav.MoveToFirstChild(); GetNodeInfo(nav); } } private static void GetNodeInfo( XPathNavigator nav1) { Console.WriteLine("Name: " +nav1.Name.ToString()); Console.WriteLine("Node Type: "+ nav1.NodeType.ToString()); Console.WriteLine("Node Value: "+nav1.Value.ToString()); if (nav1.HasChildren) { nav1.MoveToFirstChild(); while( nav1.MoveToNext() ) { GetNodeInfo(nav1); nav1.MoveToParent(); } } else { nav1.MoveToNext(); GetNodeInfo(nav1); } }
}</source>
Select a node
<source lang="csharp">using System; using System.Xml.XPath; using System.Xml; class MainClass {
static void Main(string[] args) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"c:\books.xml"); XPathNavigator nav = xmlDoc.CreateNavigator(); Console.WriteLine("Author First Name"); XPathNodeIterator itrator = nav.Select("descendant::first-name"); while( itrator.MoveNext() ) { Console.WriteLine(itrator.Current.Value.ToString()); } }
}</source>
<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.Xml.XPath; class MainClass {
static void Main(string[] args) { string xml = "<Order>" + "<Item>" + "<Description>Some widget part</Description>" + "<Price>12.99</Price>" + "</Item>" + "<Item>" + "<Description>Another widget</Description>" + "<Price>50.12</Price>" + "</Item>" + "</Order>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XPathNavigator nav = doc.CreateNavigator(); Console.WriteLine("Total price for this order is ${0}", nav.Evaluate("sum(Order/Item/Price)")); }
}</source>
Total price for this order is $63.11
<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.Xml.XPath; class MainClass {
static void Main(string[] args) { string xml = "<Order>" + "<Item>" + "<Description>widget part</Description>" + "<Price>1.9</Price>" + "</Item>" + "<Item>" + "<Description>Another widget</Description>" + "<Price>5.2</Price>" + "</Item>" + "</Order>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator nodes = nav.Select("/Order/Item[Price>10]/Description"); while (nodes.MoveNext()) { Console.WriteLine("Item {0} has a price greater than 10",nodes.Current.Value); } }
}</source>
XPathNodeIterator from a select-node statement
<source lang="csharp">using System; using System.Xml.XPath; using System.Xml; class MainClass {
static void Main(string[] args) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"c:\books.xml"); XPathNavigator nav = xmlDoc.CreateNavigator(); Console.WriteLine("Author First Name"); XPathNodeIterator itrator = nav.Select("descendant::first-name"); while( itrator.MoveNext() ) { Console.WriteLine(itrator.Current.Value.ToString()); } }
}</source>