XElement root = new XElement("root"); for (int i = 0; i < dgvBill.Rows.Count; i++) { XElement note = new XElement("Data"); for (int r = 0; r < dgvBill.Columns.Count; r++) { System.Xml.Linq.XElement xe = new XElement(dgvBill.Columns[r].HeaderText,dgvBill.Rows[i].Cells[r].Value.ToString()); note.Add(xe); } root.Add(note); } root.Save(filePath);原文链接:https://www.f2er.com/xml/294644.html