using System.Xml;
using System.Xml.Linq;
using System.IO;
// Load XML document from disk
XDocument doc = XDocument.Load("XmlData.xml");
// The new document to hole the modified XML
XDocument outDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
// Linq query to get all the trucks elements
var results = from ele in doc.Descendants("trucks")
select ele;
// Add the elements found in the result set.
foreach (XElement ele in results)
{
outDoc.Add(ele);
}
// Create a byte array from the outDoc document
byte[] xmlBytes = new byte[outDoc.ToString().Length];
xmlBytes = Encoding.UTF8.GetBytes(outDoc.ToString());
// Read it into a memory stream to use with data set
MemoryStream ms = new MemoryStream(xmlBytes);
// Create the new data set
DataSet ds = new DataSet();
// Read the XML into the data set
ds.ReadXml(ms);
// Display the info in a data grid view
dataGridView1.DataSource = ds.Tables[0];
|