June 24, 2009 at 9:17 am
Hi, I am not sure this would be the correct forum but any direction is highly appreciated. I need to parse an XML file to diplay the contents in a readable format in a windows app. The XML file contains a resume. All I could get it to do so far is to bind it as a dataset in gridview. But it would be nice to see it in a text area with some formatting. Could anyone please point me in the right direction on how to do this? Thanks in advance.
June 24, 2009 at 11:30 am
shree dhavale (6/24/2009)
Hi, I am not sure this would be the correct forum
Truly not but I think I can help 😀
I need to parse an XML file to diplay the contents in a readable format in a windows app. The XML file contains a resume. All I could get it to do so far is to bind it as a dataset in gridview. But it would be nice to see it in a text area with some formatting. Could anyone please point me in the right direction on how to do this? Thanks in advance.
If you are able to define the XML and since your destination is a DataSet you can use the DataSet.ReadXml method to read the data. To ensure correct data format you can use method DataSet.ReadXmlSchema
If you are not able to define the XML format but it's structure is just slightly different to a DataSet XML try the System.Xml.XmlDataDocument which provides a property to get a DataSet. Load the XML, change the structure and use the resulting DataSet
If your XML looks very different you can use
* A System.Xml.XmlDocument and use SelectNodes with XPath to iterate through the nodes and build the DataSet
* A System.Xml.XmlReader read through and build your DataSet
* A System.Linq.XDocument and use LINQ to build your DataSet
Hope this helps
Flo
June 24, 2009 at 12:01 pm
Thank you so much. I will give this a try and post a reply when it works!
June 24, 2009 at 12:04 pm
Glad I could help 🙂
If you need help with one of those approaches let me know
June 25, 2009 at 8:13 am
Hello again,
I tried using the select node approach but I dont think I am using the correct syntax. I used a code sample from http://support.microsoft.com/kb/318499 for this but cant get it to work.
I am inserting the xml that I am trying to parse. Greatly appreciate all your help.
Shree
Dhavale
June 25, 2009 at 9:50 am
Hi
Take the XML from the MSDN sample page and save it to disk.
With XmlDocument this would work to create a DataTable:
// using System.Xml
DataTable table = new DataTable();
table.Columns.Add("Title", typeof(string));
table.Columns.Add("Publisher", typeof(string));
XmlDocument dom = new XmlDocument();
dom.Load(@"C:\Users\Flo\Temp\Test\test.xml");
XmlNodeList bookList = dom.SelectNodes("Books/Book");
foreach (XmlElement book in bookList)
{
DataRow row = table.NewRow();
XmlText txt;
txt = (XmlText)book.SelectSingleNode("Title/text()");
row["Title"] = txt.Value;
txt = (XmlText)book.SelectSingleNode("Publisher/text()");
row["Publisher"] = txt.Value;
table.Rows.Add(row);
}
Personally I prefer LINQ and anonymous types for this:
DataTable table2 = new DataTable();
table2.Columns.Add("Title", typeof(string));
table2.Columns.Add("Publisher", typeof(string));
// using System.Xml.Linq
// using System.Xml.XPath
XDocument xdoc = Xdocument.Load(@"C:\Users\Flo\Temp\Test\test.xml");
var xBooks =
from xelement in xdoc.XPathSelectElements("Books/Book")
select new
{
Title = xelement.Element("Title").Value,
Publisher = xelement.Element("Publisher").Value
};
foreach (var item in xBooks)
{
DataRow row = table2.NewRow();
row["Title"] = item.Title;
row["Publisher"] = item.Publisher;
table2.Rows.Add(row);
}
Greets
Flo
June 25, 2009 at 11:37 am
Thank you so much for your time and detailed instructions. It worked this time!
Thanks again.
Shree
June 25, 2009 at 11:38 am
Glad we could help! 🙂
March 14, 2015 at 1:32 am
More XML Parser examples...
Nick
March 14, 2015 at 11:43 am
nickwelhar (3/14/2015)
More XML Parser examples...Nick
Thanks for that (and I'm serious. Thank you very much!). I'm not a C# programmer but, as a DBA, I've always wanted to know if parsing XML in the front-end was any easier or not. Now I know. It's not.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply