|
大哥们,帮我看看吧 我的目的是:读取xml中的数据 然后展示在DataGridView控件上,然后 通过对DataGridView上添加数据、保存数据、修改数据 让数据写回到xml 中。 在这里我用的是DataSet类。语言C# 开发工具 VS.NET 2005 代码:
class XmlTool { /** * 在窗体初始化时 解析xml取出 初始数据 返回 * 调用此方法前:验证文件存在 */ public ArrayList initXML(string file_path) { XmlDocument XMLDom = new XmlDocument(); XMLDom.Load(file_path); XmlNodeList newXMLNodes = XMLDom.SelectNodes("/config/conStrings/conString"); ArrayList arrayList = new ArrayList(); //存储返回值 用户名 密码 等。。 foreach (XmlNode xn in newXMLNodes) { //string title = xn.SelectSingleNode("User_ID").InnerXml; XmlNode userNode = xn.SelectSingleNode("User_ID"); XmlElement xe = (XmlElement)userNode; string lastedUsed = xe.GetAttribute("lasted_used"); //原则上只有一个lasted_used属性 if (lastedUsed.Length > 0 & "true".Equals(lastedUsed,StringComparison.CurrentCultureIgnoreCase)) { string ds = xn.SelectSingleNode("Data_Source").InnerXml; //服务名 string user = xe.InnerXml; //用户名 string pwd = xn.SelectSingleNode("Password").InnerXml; //密码 string unicode = xn.SelectSingleNode("Unicode").InnerXml; //Unicode string security = xn.SelectSingleNode("Integrated_Security").InnerXml; //继承验证 string info = xn.SelectSingleNode("Persist_Security_Info").InnerXml; //敏感保护 //增加到List列表 arrayList.Add(ds); arrayList.Add(user); arrayList.Add(pwd); arrayList.Add(unicode); arrayList.Add(security); arrayList.Add(info); //save //XMLDom.Save(file_path); //MessageBox.Show("找到对应的值了"); return arrayList; } else { //将最后一个值返回 XmlNode lst_xmlNode = newXMLNodes.Item(newXMLNodes.Count - 1); string ds = lst_xmlNode.SelectSingleNode("Data_Source").InnerXml; //服务名 string user = lst_xmlNode.SelectSingleNode("User_ID").InnerXml; //用户名 string pwd = lst_xmlNode.SelectSingleNode("Password").InnerXml; //密码 string unicode = lst_xmlNode.SelectSingleNode("Unicode").InnerXml; //Unicode string security = lst_xmlNode.SelectSingleNode("Integrated_Security").InnerXml; //继承验证 string info = lst_xmlNode.SelectSingleNode("Persist_Security_Info").InnerXml; //敏感保护 //增加到List列表 arrayList.Add(ds); arrayList.Add(user); arrayList.Add(pwd); arrayList.Add(unicode); arrayList.Add(security); arrayList.Add(info); //save //XMLDom.Save(file_path); //返回值 return arrayList; //out // MessageBox.Show(ds); } } return null; } /** * 判断给定的文件是否以xml结尾 * */
/** * 给定的事件 * */ public DataSet xmlLoad(string file_path) { DataSet ds = null; if (File.Exists(file_path)) { ds = new DataSet(); try { ds.ReadXml(file_path); } catch (XmlException ex) { MessageBox.Show(ex.Message,"提示"); return null; } catch (IndexOutOfRangeException ex1) { MessageBox.Show(ex1.Message,"文件加载出错"); return null; } } else { MessageBox.Show("文件不存在", "提示"); } return ds; } /** * 给定文件路径,如果改路径存在,则找到改路径所在的目录 * */ public string getDirPath(string filePath){ if (filePath.Length > 0 && File.Exists(filePath)) { FileStream ff = File.Create(filePath); } return null; }
}
//// 窗体的初始化事件里面 加载xml文件
private void Query_Load(object sender, EventArgs e) { String file_path = txt_xml_path.Text; if (!File.Exists(file_path)) { //隐藏搜索按钮 btn_search.Hide(); } dataGridView1.AutoGenerateColumns = false; XmlTool tool = new XmlTool(); ds = tool.xmlLoad(file_path); //加载数据 dataGridView1.DataSource = ds.Tables[1].DefaultView; } // 添加按钮里面 增加记录
private void btn_Add_Click(object sender, EventArgs e) { DataView dv = ds.Tables[0].DefaultView; dv.AddNew(); dataGridView1.DataSource = dv; dataGridView1.CurrentCell.Selected = false; dataGridView1.Rows[dataGridView1.RowCount-1].Selected = true; dataGridView1.FirstDisplayedScrollingRowIndex= dataGridView1.RowCount - 1; }
这个代码能够添加记录到控件上去,但保存会出错 。 用调试 打印输出看一下 发现 记录添加到 xml文件的结尾。
|
一共有 19 条评论
微软的工程师说 他也搞不定
DataSet 不支持在内存当中两个表的外键关联 所以麽办法啊
哈哈 后来
干脆用原始的方法 解析的 ~~