XML Parsing

Last post 07-03-2009 1:17 AM by kavita_khandhadia. 2 replies.

Sort Posts:

  • XML Parsing

    07-02-2009, 3:01 PM
    • Member
      37 point Member
    • davidtran
    • Member since 10-06-2006, 4:04 PM
    • Posts 24

    I would like to parse an unusually formatted xml file, I'm pretty new at this so pointers would be helpful.

    My XML file looks something like this.

    <report>
    	<table>
    		<columns>
    			<column name="Name"/>
    			<column name="Age"/>
                   </columns>
                   <rows>
                            <row Name="Jackie Chan" Age="53" />
                            <row Name="Tim Burton" Age="65" />
                            <row Name="Steven Segal" Age="52" />
                            etc.....
                   </rows>
            </table>
    <totals>
           <subtotal films="5" rating="4" views="12000">
           etc....
    </totals>
    </report>
    
    

    How would I in C# get the column names inside of "row" and the values of "Name" and "Age" in each row?

    I would like to take this data and hold it in a dataset to insert into MSSQL.

    Can anyone give me some pointers please?

    Thanks

    David


  • Re: XML Parsing

    07-02-2009, 4:08 PM
    Answer
    • Member
      326 point Member
    • _voidstar_
    • Member since 06-15-2009, 4:06 PM
    • Posts 53

     Here's a good article about selecting XML data using XPath:

    http://support.microsoft.com/kb/308333

    ...and if you have any questions about XPath, just search google.  Or check out http://www.w3schools.com/Xpath/.

     

     

  • Re: XML Parsing

    07-03-2009, 1:17 AM
    Answer

    Read my blog for solving this problem

    http://kavstech.blogspot.com/2009/07/converting-datatabledateset-to-xml-and.html


    if u dint get it.. let me know..see the example i have written for u...


    string fileName = Server.MapPath("XMLFile3.xml");
            XmlDocument xDoc = new XmlDocument();

            string userRightsXml = string.Empty;
            xDoc.Load(fileName);

            //Creatting a DataTable from this XML. To add this datatable to dataset.

            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Age");
            DataRow dr;
           

            XmlNodeList xNodeList = xDoc.SelectNodes("report/table/rows/row");

            foreach (XmlNode xNode in xNodeList)
            {
                if (xNode.Name == "row")
                {
                    string columnName = xNode.Attributes["Name"].Value;
                    string age = xNode.Attributes["Age"].Value;
                    dr = dt.NewRow();
                    dr["Name"] = columnName;
                    dr["Age"] = age;
                    dt.Rows.Add(dr);              
                }
            }

            //now add the Datatable to dataset

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);


    Please mark this post as Answer if it is of help to you!

    " Every wall is a door..! "
    Filed under:
Page 1 of 1 (3 items)