62,046
社区成员
发帖
与我相关
我的任务
分享
var strXml="你的XML字符串";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXml);
XmlNodeReader reader = new XmlNodeReader(doc);
DataSet ds = new DataSet();
ds.ReadXml(reader);
reader.Close();
dg.DataSource = ds.Tables[0].DefaultView;
dg.DataBind();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
namespace Interview {
class XmlToDataset {
public XmlDocument loadXml() {
var xml = @"<?xml version=""1.0"" encoding=""utf-16"" standalone=""no""?>
<AssignmentInfo>
<AttributeInfo>
<AttrType>DEPT_CODE</AttrType>
<AttrText>所属部门</AttrText>
<AttrValue>部门001</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>BTYPE</AttrType>
<AttrText>血型</AttrText>
<AttrValue>B型</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>SCODE</AttrType>
<AttrText>性别</AttrText>
<AttrValue>男</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>ECODE</AttrType>
<AttrText>学历</AttrText>
<AttrValue>本科</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>DECODE</AttrType>
<AttrText>学位</AttrText>
<AttrValue>学士</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>NACODE</AttrType>
<AttrText>民族</AttrText>
<AttrValue>汉族</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>PCODE</AttrType>
<AttrText>政治面貌</AttrText>
<AttrValue>群众</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>GRCODE</AttrType>
<AttrText>工人技术等级</AttrText>
<AttrValue>A级</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>JOBS_CODE</AttrType>
<AttrText>工种</AttrText>
<AttrValue>短工</AttrValue>
<valueDesc />
</AttributeInfo>
<AttributeInfo>
<AttrType>LCCODE</AttrType>
<AttrText>用工类别</AttrText>
<AttrValue>类别001</AttrValue>
<valueDesc />
</AttributeInfo>
</AssignmentInfo>";
var xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
return xmldoc;
}
public DataSet ToDataSet(XmlDocument xmlDoc) {
DataTable table = new DataTable();
table.Columns.Add("AttrType", typeof(String));
table.Columns.Add("AttrText", typeof(String));
table.Columns.Add("AttrValue", typeof(String));
table.Columns.Add("valueDesc", typeof(String));
var nodes = xmlDoc.DocumentElement.SelectNodes("//AttributeInfo");
foreach (XmlNode node in nodes) {
var row = table.NewRow();
row[0] = node.ChildNodes[0].InnerText;
row[1] = node.ChildNodes[1].InnerText;
row[2] = node.ChildNodes[2].InnerText;
row[3] = node.ChildNodes[3].InnerText;
table.Rows.Add(row);
}
DataSet ds = new DataSet();
ds.Tables.Add(table);
return ds;
}
}
}