http://bbs.csdn.net/topics/390285839
没人答复,我就转来这边了~~帮忙看下哈
以下是方法返回的xml格式数据格式:
我想 把他们转换成对应的三个table表存放到数据库中
(学生表\初中成绩表\高中成绩表)678910111213141516171819202122 <Student> <Student Sname="李四 " Sage="15 " Ssex="女"> <初中成绩> <Course>化学</Course> <Score>77</Score> <Course>地理</Course> <Score>78</Score> <Course>物理</Course> <Score>88</Score> </初中成绩> <高中成绩> <Course>语文</Course> <Score>90</Score> <Course>数学</Course> <Score>99</Score> <Course>英语</Course> <Score>88</Score> </高中成绩> </Student>[/align]<Student Sname="王五" Sage="16" Ssex="男">以下同样的格式
xDoc.Load(System.Web.HttpContext.Current.Server.MapPath("文件路径"));
XmlNodeList xnl = xDoc.GetElementsByTagName("Student");
foreach (XmlNode xd in xnl)
{
//xd.Attributes["Sname"].Value
//xd.InnerText
}
如果能改。就先定义好3个datatable
然后用一个dataset添加这3个datatable。
然后直接用dataset存储为xml .选择保存数据和结构那种。然后直接用 dataset读xml即可如果不能改格式。
那你就老老试试的找到<Student> <Course> <Score> 这样的节点。
然后找里面的element 或者attribute值。
上面那个xml格式是调用一个方法的返回值.其实我就想根据引用这个方法,然后将对应的值都放到对应的表数据中.如ttt表( Sname,Sage,Ssex),初中成绩表(Sname,Course,Score)和高中成绩表(Sname,Course,Score)中,并绑定到数据控件GridView上显示能帮我写个方法吗
1. XmlNode node = pXml.SelectSingleNode("message/result");
2.XmlNodeList xmlObjList = pXml.SelectNodes("message/object");
3. XmlNode objIDNode = nodeObj.Attributes.GetNamedItem("id");
4.msdn查xmldocument
另,有个前提是未知xmldocument类型各个节点等信息,要把里面所有表都循环出来
你可以自己递归。或是自己用 xpath
或是你用反序列化。例如:[Serizale]
[XmlRoot(.....)]
public seald class Student
{
[XmlElement(ElementName="ID")]
ID{get;set;} [XmlArray]
[XmlArrayItem]
}最后你在用 System.XmlSerizale 进行序列化。
XmlDocument doc = new XmlDocument();
doc = ws.xd_GetM_Grade111(); XmlNode root = doc.SelectSingleNode(doc.DocumentElement.Name);
DataTable dt = new DataTable();
dt.TableName = "初中成绩";
//dt.Columns.Add(new DataColumn(root.ChildNodes[0].Attributes[0].Name, typeof(string)));
XmlNode node_1_1 = doc.SelectSingleNode(doc.DocumentElement.Name + "/" + doc.DocumentElement.FirstChild.Name + "/" + doc.DocumentElement.FirstChild.FirstChild.Name);
XmlNodeList nodelist_1_1 = node_1_1.ChildNodes; for (int i = 0; i < 2; i++)
{
dt.Columns.Add(new DataColumn(node_1_1.ChildNodes[i].Name, typeof(string)));
}
DataRow dr = dt.NewRow();
foreach (XmlNode xn in nodelist_1_1)
{
foreach (DataColumn column in dt.Columns)
{
if (xn.LocalName == column.ColumnName)
{
dr[column] = xn.InnerText.ToString().Trim();
dt.Rows.Add(dr);
}
}
}
显示结果为
Course Score
化学
77
地理
78
物理
88
以上表格,一一对应的科目和分数都成了两行了
如何更改代码,使结果显示正常