简体   繁体   中英

reading from xml file into datatable and grid view

my xml is like below

<Expression>
    <Field>X</Field>
    <Formula>a+b</Formula>          
</Expression>
<Expression>
    <Field>Y</Field>
    <Formula>a-b</Formula>          
</Expression>
<Expression>
    <Field>Z</Field>
    <Formula>a*b</Formula>          
</Expression>

my c# page

DataSet ds = new DataSet();           
ds.ReadXml(@"C:\\Test.xml");
GridView1.DataSource = ds;
GridView1.DataMember = ds.Tables[0].ToString();
GridView1.DataBind();

it keeps showing me error:

There are multiple root elements. Line 6, position 4.

Xml file must have single root element , which have other elements as children. Currently you have several Expression elements on root level. Wrap them into some Expressions element to make your xml file valid:

<Expressions>
  <Expression>
    <Field>X</Field>
    <Formula>a+b</Formula>
  </Expression>
  <Expression>
    <Field>Y</Field>
    <Formula>a-b</Formula>
  </Expression>
  <Expression>
    <Field>Z</Field>
    <Formula>a*b</Formula>
  </Expression>
</Expressions>

Your xml loading code is valid. It creates DataSet with single DataTable named Expression . That table has two columns ( Field and Formula ) and three rows with data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM