using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
public class MainClass {
public static void Main() {
XPathDocument doc = new XPathDocument("books.xml");
XPathNavigator nav = ((IXPathNavigable)doc).CreateNavigator();
XPathNodeIterator iter = nav.Select("/bookstore/book[@genre='novel']");
while (iter.MoveNext()) {
XPathNodeIterator newIter = iter.Current.SelectDescendants(XPathNodeType.Element, false);
while (newIter.MoveNext())
Console.WriteLine(newIter.Current.Name + ": " + newIter.Current.Value);
}
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Add XmlElement
//Microsoft Public License (Ms-PL)
//http://dbmlmanager.codeplex.com/license
#region using
using System;
using System.Xml;
#endregion
namespace DbmlManager.Lib.Utility
{
#region Class Docs
/// <summary>
/// Summary description for XmlUtil.
/// </summary>
#endregion
public class XmlUtil
{
// Public Static Methods
#region AddXmlElement(XmlDocument xml, XmlNode parent, string nodeName, string nodeText)
public static void AddXmlElement(XmlDocument xml, XmlNode parent, string nodeName, string nodeText)
{
XmlElement node = xml.CreateElement(nodeName);
node.InnerText = nodeText;
parent.AppendChild(node);
}
#endregion
}
}
Find Elements with an XPath Search
using System;
using System.Xml;
public class XPathSelectNodes {
private static void Main() {
// Load the document.
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
XmlNodeList nodes = doc.SelectNodes("/books/A/B");
foreach (XmlNode node in nodes) {
Console.WriteLine(node.InnerText);
}
}
}
/*
<books>
<A property="a">
<B>text</B>
<C>textg</C>
<D>99999</D>
</A>
</books>
*/
Get XML Nodes in a Specific XML Namespace
using System;
using System.Xml;
public class SelectNodesByNamespace {
private static void Main() {
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
XmlNodeList matches = doc.GetElementsByTagName("*","http://mycompany/OrderML");
foreach (XmlNode node in matches) {
Console.Write(node.Name + " ");
foreach (XmlAttribute attribute in node.Attributes) {
Console.Write(attribute.Value + " ");
}
}
}
}
Use SelectNodes to query nodes by XPath
using System;
using System.Xml;
class MainClass {
private static void Main() {
XmlDocument doc = new XmlDocument();
doc.Load("orders.xml");
XmlNodeList nodes = doc.SelectNodes("/Order/Items/Item/Name");
foreach (XmlNode node in nodes) {
Console.WriteLine(node.InnerText);
}
Console.ReadLine();
}
}
Use Load method in XmlDocument to load xml document
//Order.xml
/*
<?xml version="1.0" ?>
<ord:order xmlns:ord="http://mycompany/OrderML"
xmlns:cli="http://mycompany/ClientML">
<cli:client>
<cli:firstName>Sally</cli:firstName>
<cli:lastName>Sergeyeva</cli:lastName>
</cli:client>
<ord:orderItem itemNumber="3211"/>
<ord:orderItem itemNumber="1155"/>
</ord:order>
*/
using System;
using System.Xml;
class MainClass {
public static void Main() {
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
XmlNodeList matches = doc.GetElementsByTagName("*", "http://mycompany/OrderML");
foreach (XmlNode node in matches) {
Console.Write(node.Name + " ");
foreach (XmlAttribute attribute in node.Attributes) {
Console.Write(attribute.Value + " ");
}
}
}
}
Call GetElementsByTagName to get an element
using System;
using System.Xml;
public class MainClass {
private static void Main() {
XmlDocument doc = new XmlDocument();
doc.Load("ProductCatalog.xml");
XmlNodeList prices = doc.GetElementsByTagName("productPrice");
decimal totalPrice = 0;
foreach (XmlNode price in prices) {
totalPrice += Decimal.Parse(price.ChildNodes[0].Value);
}
Console.WriteLine("Total catalog value: " + totalPrice.ToString());
}
}