刚来公司的时候,Boss让我修改以前的程序,主要是根据节点特征读取xml中特定字节并生成一个字符串。
XML节点如下:
<Columns>
<Name>NAME</Name>
<Index>0</Index>
<Description>姓名</Description>
<ShowIndex>0</ShowIndex>
<ShowOrNot />
</Columns>
<Columns>
<Name>AGE</Name>
<Index>1</Index>
<Description>年龄</Description>
<ShowIndex>1</ShowIndex>
<ShowOrNot>0</ShowOrNot>
</Columns>
<Columns>
<Name>CLASS</Name>
<Index>2</Index>
<Description>班级</Description>
<ShowIndex>2</ShowIndex>
<ShowOrNot />
</Columns>
<Columns>
<Name>NUM</Name>
<Index>3</Index>
<Description>学号</Description>
<ShowIndex>3</ShowIndex>
<ShowOrNot>0</ShowOrNot>
</Columns>
<Columns>
<Name>SEX</Name>
<Index>4</Index>
<Description>性别</Description>
<ShowIndex>4</ShowIndex>
<ShowOrNot />
</Columns>
处理方法:
DataSet ds = new DataSet();
ds.ReadXml("D://test.xml");
DataTable dt = new DataTable();
string strDes = "";
//设置DataTable的表结构与XML结构相同
for(int i=0;i<ds.Tables[0].Columns.Count;i++)
{
dt.Columns.Add(ds.Tables[0].Columns[i].ColumnName);
}
//根据ShowOrNot节点判断是否取出Description节点数据,
//并按照ShowIndex排列顺序
foreach(DataRow dr in ds.Tables[0].Select("ShowOrNot not in('0')","ShowIndex Asc"))
{
dt.ImportRow(dr);
strDes += dr["Description"]+",";
}
strDes = strDes.Substring(0,strDes.Length-1);
MessageBox.Show(strDes);
dataGrid1.DataSource= dt;