CSharp examples for System.Xml:XML Document
Returns a DataTable for all rows on the path from XmlDocument
// All rights reserved. using System.Diagnostics; using System.Collections.Specialized; using System.Xml; using System.Data; using System;/* w w w. ja v a2 s . c o m*/ public class Main{ public static DataTable Query(XmlDocument doc, int startChildNode, string xpath) { VerifyParameters(doc, xpath); DataTable dt = new DataTable(); XmlNodeList nodeList = startChildNode == 0 ? doc.LastChild.SelectNodes(xpath) : doc.ChildNodes[startChildNode].LastChild.SelectNodes(xpath); if (nodeList.Count != 0) { CreateColumns(dt, nodeList[0]); } foreach (XmlNode node in nodeList) { DataRow dr = dt.NewRow(); foreach (XmlAttribute attr in node.Attributes) { dr[attr.Name] = attr.Value; } dt.Rows.Add(dr); } return dt; } /// <summary> /// Returns a DataTable for all rows on the path. /// </summary> /// <param name="xpath">The xml path.</param> /// <returns>The DataTable with the returned rows. /// The row count will be 0 if no rows returned.</returns> /// <exception cref="System.ArgumentNullException">Thrown when an argument is null.</exception> /// <remarks>Additional exceptions may be thrown by the XmlDocument class.</remarks> public static DataTable Query(XmlDocument doc, string xpath) { return Query(doc, 0, xpath); } }