导入内容视图:

代码:
string filePath = AppDomain.CurrentDomain.BaseDirectory + "orderDownload.xml"; if (File.Exists(filePath)) { File.Delete(filePath); } //创建新的XML XmlDocument xmlDoc = xmlop.LoadXml(filePath, "Root"); xmlop.DeleteXmlElement(filePath, xmlDoc, "Root", "", "", true); foreach (DataGridViewRow row in this.dgvDetails.Rows) { if (row.Cells["选择"].Value != null && row.Cells["选择"].Value.ToString().ToLower() == "true") { Order OrderModel = new Order(); OrderModel.AutoID = Convert.ToInt32(row.Cells["_AutoID"].Value.ToString().Trim()); OrderModel.Code = row.Cells["Code"].Value.ToString().Trim(); OrderModel.CustomerCode = row.Cells["CustomerCode"].Value.ToString().Trim(); OrderModel.CustomerName = row.Cells["CustomerName"].Value.ToString().Trim(); OrderModel.OrderTime = row.Cells["OrderTime"].Value.ToString().Trim(); OrderModel.Address = row.Cells["Address"].Value.ToString().Trim(); OrderModel._IsComplete = Convert.ToInt32(row.Cells["_IsComplete"].Value.ToString().Trim()); OrderModel.IsComplete = row.Cells["IsComplete"].Value.ToString().Trim(); OrderModel.Remark = row.Cells["Remark"].Value.ToString().Trim(); xmlop.AddXmlElement<Order>(filePath, xmlDoc, "Root", OrderModel); } } /// <summary> /// 添加XML节点 /// </summary> /// <typeparam name="T">对象</typeparam> /// <param name="filePath">文件路径</param> /// <param name="xmlDoc">XmlDocument对象</param> /// <param name="root_node">根节点</param> /// <param name="t">对象</param> public void AddXmlElement<T>(string filePath,XmlDocument xmlDoc,string root_node,T t) where T : new() { XmlNode xmlDocSelect = xmlDoc.SelectSingleNode(root_node); XmlElement xmlEle = xmlDoc.CreateElement(t.GetType().Name); //获取对象属性 PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); //遍历对象的属性,并获取属性的值 foreach (PropertyInfo item in properties) { string attName = item.Name; object value = item.GetValue(t,null); if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { XmlAttribute xmlattr = xmlDoc.CreateAttribute(attName); if (value == null) { xmlattr.Value = ""; } else { xmlattr.Value = value.ToString(); } xmlEle.Attributes.Append(xmlattr); } } xmlDocSelect.AppendChild(xmlEle); xmlDoc.Save(filePath); }
浙公网安备 33010602011771号