Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<pre lang="xml"><Parts>
-   <Part>
        <Section>3003512</Section>
        <Mark>RP-103</Mark>
        <Length>4950</Length>
        - <Components>
                <Section>3003512</Section>
                <Mark>RP-103</Mark>
                <Length>4950</Length>
                <Remark>System Generated </Remark>
              <Components />
            <Remark>No Comments </Remark>
        </Part>

-   <Part>
        <Section>3003512</Section>
        <Mark>RP-103</Mark>
        <Length>4950</Length>
        <Components />
        <Remark>No Comments </Remark>
     </Part>
</Parts>


I want to Read Only Sections and Mark in Tabular Format. I am using below code to read this But it is giving Error Table Schema 'Component' Already Exists .

C#
DataTable dt = new DataTable();
        DataColumn dc = new DataColumn("Mark");
        DataColumn dc1 = new DataColumn("Sections ");
        dt.Columns.Add(dc);
        dt.Columns.Add(dc1);
        DataSet dSet = new DataSet();


        if (File.Exists(xmlpath2))
        {

            XmlTextReader Reader1 = new XmlTextReader(xmlpath2);



            dSet.ReadXml(Reader1, XmlReadMode.Auto);


            for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
            {
                DataRow rows = dSet.Tables[0].Rows[i];
                DataRow myRow = dt.NewRow();
                myRow["Mark"] = rows["Mark"];
                myRow["Sections "] = rows["Sections "];

                dt.Rows.Add(myRow);
            }


            GridView1.DataSource = dt;
            GridView1.DataBind();

        }
Posted

giving Error Table Schema 'Component' Already Exists
Here: dSet.ReadXml(Reader1, XmlReadMode.Auto); OR myRow["Mark"] = rows["Mark"]; ?

If so, there are two 'mark' in your xml file. Thus access to 'mark' field in dataset table formed can be an issue.
 
Share this answer
 
Hi,

below code may be helpful to you,

C#
string xmlstring = @"<Parts>
                                  <Part>
                                       <Section>3003512</Section>
                                       <Mark>RP-103</Mark>
                                       <Length>4950</Length>
                                        <Components>
                                               <Section>3003512</Section>
                                               <Mark>RP-103</Mark>
                                               <Length>4950</Length>
                                               <Remark>System Generated </Remark>
                                        </Components>
                                        <Remark>No Comments </Remark>
                                  </Part>

                                  <Part>
                                       <Section>3003512</Section>
                                       <Mark>RP-103</Mark>
                                       <Length>4950</Length>
                                       <Components />
                                       <Remark>No Comments </Remark>
                                   </Part>
                               </Parts>";

           XDocument xd = XDocument.Parse(xmlstring);
           var result = xd.Descendants("Part").Select(s => new {Section = s.Element("Section"), Mark = s.Element("Mark")});


One more thing, your xml is not correct. please compare your data with my xmlstring variable value. you will find out your problem.

thanks
-Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900