前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 读写实现本地Xml文件与界面ListView的转换

C# 读写实现本地Xml文件与界面ListView的转换

作者头像
静谧的小码农
发布2019-01-11 12:36:54
1.2K0
发布2019-01-11 12:36:54
举报
文章被收录于专栏:静谧的小码农静谧的小码农
  1. 将List转换成DateTable

public static DataTable ToDataTable(IList list)

        {

DataTable result = new DataTable();

if (list.Count > 0)

            {

PropertyInfo[] propertys = list[0].GetType().GetProperties();

foreach (PropertyInfo pi in propertys)

                {

                    result.Columns.Add(pi.Name);

                }

for (int i = 0; i < list.Count; i++)

                {

ArrayList tempList = new ArrayList();

foreach (PropertyInfo pi in propertys)

                    {

object obj = pi.GetValue(list[i], null);

                        tempList.Add(obj);

                    }

object[] array = tempList.ToArray();

                    result.LoadDataRow(array, true);

                }

            }

return result;

        }

  1. 读取本地XML文件,并且存储为DateTable,且显示到界面上的ListView中

XmlDocument xmlDoc = new XmlDocument();

XmlReaderSettings settings = new XmlReaderSettings();

            settings.IgnoreComments = true;//忽略文档里面的注释

XmlReader reader = XmlReader.Create(@"e:\test.xml", settings);

            xmlDoc.Load(reader);

//得到根节点

XmlNode xn = xmlDoc.SelectSingleNode("ZMBJZ");

//得到根节点的所有子节点

XmlNodeList xnl = xn.ChildNodes;

foreach (XmlNode xn1 in xnl)

            {

cls_XTZMBJ zmbj = new cls_XTZMBJ();

// 将节点转换为元素,便于得到节点的属性值

XmlElement xe = (XmlElement)xn1;

// 得到Type和ISBN两个属性的属性值

                zmbj.ID = int.Parse(xe.GetAttribute("nID").ToString());

                zmbj.Name = xe.GetAttribute("cTBMC").ToString();

                zmbj.SSFZ = xe.GetAttribute("nSSZH").ToString();

                zmbj.TBBH = xe.GetAttribute("ZMTB").ToString();

                ZMBJModeList.Add(zmbj);

            }

DataTable dt = new DataTable();

            dt = ToDataTable(ZMBJModeList);

dataGridView1.DataSource = dt;

DataGridViewComboBoxCell cb = new DataGridViewComboBoxCell();

            cb.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;

for (int i = 0; i < dataGridView1.Rows.Count; i++)

            {

if (cb.Items.IndexOf(dataGridView1.Rows[i].Cells[2].Value.ToString()) == -1)

                {

                    cb.Items.Add(dataGridView1.Rows[i].Cells[2].Value.ToString());

                }

            }

for (int i = 0; i < dataGridView1.Rows.Count; i++)

            {

DataGridViewComboBoxCell cb1 = new DataGridViewComboBoxCell();

//cb.Items.Add(dataGridView1.Rows[i].Cells[2].Value.ToString());

for (int j = 0; j < cb.Items.Count; j++)

                {

                    cb1.Items.Add(cb.Items[j].ToString());

                }

                dataGridView1.Rows[i].Cells[2] = cb1;

            }

            reader.Close();

  1. 存储ListView内容到本地XML文件中

ZMBJModeList = new List<cls_XTZMBJ>();

foreach (DataGridViewRow dgvr in dataGridView1.Rows)

            {

cls_XTZMBJ zmbj = new cls_XTZMBJ();

// 将节点转换为元素,便于得到节点的属性值

                zmbj.ID = int.Parse(dgvr.Cells[0].Value.ToString());

                zmbj.Name = dgvr.Cells[1].Value.ToString();

                zmbj.SSFZ = dgvr.Cells[2].Value.ToString();

                zmbj.TBBH = dgvr.Cells[3].Value.ToString();

                ZMBJModeList.Add(zmbj);

            }

List<string> ListTemp = new List<string>();

foreach (cls_XTZMBJ zmbj in ZMBJModeList)

            {

                ListTemp.Add(zmbj.SSFZ);

            }

            ListTemp = ListTemp.Distinct().ToList();

int a = ListTemp.Count;

//MessageBox.Show(a.ToString());

string[] group = new string[a];

int j = 0;

foreach (string str in ListTemp)

            {

                group[j] = str;

//MessageBox.Show(group[j]);

                j++;

            }

int b = ZMBJModeList.Count;

int k = 0;

foreach (cls_XTZMBJ zmbj in ZMBJModeList)

            {

for (int l = 0; l < a; l++)

                {

//MessageBox.Show(group[l]);

//MessageBox.Show(lvi.Group.Header.ToString());

if (zmbj.SSFZ.ToString() == group[l])

                    {

if (int.Parse(zmbj.ID.ToString()) < b - 1)

                        {

                            strxml += "new XElement(\"ZMBJ\", new XAttribute(\"nID\", \"" + int.Parse(zmbj.ID.ToString()) + "\"),new XAttribute(\"cTBMC\", \"" + zmbj.Name.ToString() + "\"),new XAttribute(\"nSSZH\", \"" + group[l] + "\"),new XAttribute(\"ZMTB\", \"" + zmbj.TBBH.ToString() + "\")),";

                        }

else if (int.Parse(zmbj.ID.ToString()) == b - 1)

                        {

                            strxml += "new XElement(\"ZMBJ\", new XAttribute(\"nID\", \"" + int.Parse(zmbj.ID.ToString()) + "\"),new XAttribute(\"cTBMC\", \"" + zmbj.Name.ToString() + "\"),new XAttribute(\"nSSZH\", \"" + group[l] + "\"),new XAttribute(\"ZMTB\", \"" + zmbj.TBBH.ToString() + "\")))";

                        }

else

                        {

MessageBox.Show("error");

                        }

                    }

                }

                k++;

            }

//MessageBox.Show(strxml);

EvaluatorItem[] items = { new EvaluatorItem(typeof(XElement), strxml, "getXElement") };

Evaluator eval = new Evaluator(items);

XElement xcust = (XElement)eval.Evaluate("getXElement");

string xmlFileName = @"e:\test.xml";

            xcust.Save(xmlFileName);

MessageBox.Show("保存成功!");

this.Close();

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年01月08日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档