Parsing XML file using C# in Windows Form to display readable text.

  • shree dhavale

    SSChasing Mays

    Points: 609

    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.

  • Florian Reischl

    SSC-Dedicated

    Points: 37299

    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

  • shree dhavale

    SSChasing Mays

    Points: 609

    Thank you so much. I will give this a try and post a reply when it works!

  • Florian Reischl

    SSC-Dedicated

    Points: 37299

    Glad I could help 🙂

    If you need help with one of those approaches let me know

  • shree dhavale

    SSChasing Mays

    Points: 609

    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

  • Florian Reischl

    SSC-Dedicated

    Points: 37299

    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

  • shree dhavale

    SSChasing Mays

    Points: 609

    Thank you so much for your time and detailed instructions. It worked this time!

    Thanks again.

    Shree

  • Florian Reischl

    SSC-Dedicated

    Points: 37299

    Glad we could help! 🙂

  • nickwelhar

    Valued Member

    Points: 61

    More XML Parser examples...

    Nick

  • Jeff Moden

    SSC Guru

    Points: 996655

    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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "If "pre-optimization" is the root of all evil, then what does the resulting no optimization lead to?"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply