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

2019-01-11 12:36:54 浏览数 (1)

  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();

0 人点赞