问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Xml;usingSystem.Xml.Linq;namespaceOperateXML{publicpartialclassFrm_Main:Form{publicFrm_Main(){InitializeComponent();}staticstringstrPath="Employee.xml";staticstringstrID="";//窗体加载时加载XML文件privatevoidForm1_Load(objectsender,EventArgse){if(File.Exists(strPath)){groupBox1.Enabled=false;getXmlInfo();}elsegroupBox1.Enabled=true;}//创建XML文件privatevoidbutton1_Click(objectsender,EventArgse){XDocumentdoc=newXDocument(newXDeclaration("1.0","utf-8","yes"),newXElement(textBox1.Text,newXElement(textBox2.Text,newXAttribute(textBox3.Text,textBox10.Text),newXElement(textBox4.Text,textBox5.Text),newXElement(textBox6.Text,textBox7.Text),newXElement(textBox8.Text,textBox9.Text))));doc.Save(strPath);groupBox1.Enabled=false;getXmlInfo();}//添加XML元素privatevoidbutton2_Click(objectsender,EventArgse){XElementxe=XElement.Load(strPath);IEnumerable<XElement>elements1=fromelementinxe.Elements("People")selectelement;//生成新的编号stringstr=(Convert.ToInt32(elements1.Max(element=>element.Attribute("ID").Value))+1).ToString("000");XElementpeople=newXElement("People",newXAttribute("ID",str),newXElement("Name",textBox11.Text),newXElement("Sex",comboBox1.Text),newXElement("Salary",textBox12.Text));xe.Add(people);xe.Save(strPath);getXmlInfo();}//修改XML元素privatevoidbutton3_Click(objectsender,EventArgse){if(strID!=""){XElementxe=XElement.Load(strPath);IEnumerable<XElement>elements=fromelementinxe.Elements("People")whereelement.Attribute("ID").Value==strIDselectelement;if(elements.Count()>0){XElementnewXE=elements.First();newXE.SetAttributeValue("ID",strID);newXE.ReplaceNodes(newXElement("Name",textBox11.Text),newXElement("Sex",comboBox1.Text),newXElement("Salary",textBox12.Text));}xe.Save(strPath);}getXmlInfo();}//删除XML元素privatevoidbutton4_Click(objectsender,EventArgse){if(strID!=""){XElementxe=XElement.Load(strPath);IEnumerable<XElement>elements=fromelementinxe.Elements("People")whereelement.Attribute("ID").Value==strIDselectelement;if(elements.Count()>0)elements.First().Remove();xe.Save(strPath);}getXmlInfo();}//显示选中XML节点的详细信息privatevoiddataGridView1_CellClick(objectsender,DataGridViewCellEventArgse){strID=dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();XElementxe=XElement.Load(strPath);IEnumerable<XElement>elements=fromPInfoinxe.Elements("People")wherePInfo.Attribute("ID").Value==strIDselectPInfo;foreach(XElementelementinelements){textBox11.Text=element.Element("Name").Value;comboBox1.SelectedItem=element.Element("Sex").Value;textBox12.Text=element.Element("Salary").Value;}}#region将XML文件内容绑定到DataGridView控件///<summary>///将XML文件内容绑定到DataGridView控件///</summary>privatevoidgetXmlInfo(){DataSetmyds=newDataSet();myds.ReadXml(strPath);dataGridView1.DataSource=myds.Tables[0];}#endregionprivatevoidtextBox1_TextChanged(objectsender,EventArgse){}privatevoidgroupBox1_Enter(objectsender,EventArgse){}privatevoidlabel1_Click(objectsender,EventArgse){}}}现在有一个程序,只能修改给的XML文件,想做成一个能读取这个XML文件的程序,求大神修改<ammoname="tank125"class="Rocket"><geometry></geometry><flags><paramname="serverspawn"value="1"/><paramname="predictspawn"value="1"/></flags><physicstype="particle"><paramname="mass"value="0.35"/><paramname="speed"value="350"/><paramname="radius"value="0.25"/><paramname="air_resistance"value="0"/><paramname="water_resistance"value="0.5"/><paramname="gravity"value="0,0,-9.81"/><paramname="water_gravity"value="0,0,-1"/><paramname="thrust"value="0"/><paramname="lift"value="0"/><paramname="min_bounce_speed"value="8"/><paramname="material"value="mat_metal"/><paramname="pierceability"value="8"/><paramname="single_contact"value="1"/></physics><explosion><paramname="pressure"value="1600"/><paramname="min_radius"value="3"/><paramname="max_radius"value="5"/><paramname="min_phys_radius"value="1.8"/><paramname="max_phys_radius"value="2.5"/><paramname="hole_size"value="2"/><paramname="terrain_hole_size"value="6"/></explosion><params><paramname="lifetime"value="5"/><paramname="showtime"value="0.15"/></params><trail><paramname="effect"value="weapon_fx.tank.tank125.tracer.tracer"/><paramname="scale"value="0.35"/></trail></ammo>
解决方案
解决方案二:
http://bbs.csdn.net/topics/390027179
解决方案三:
已经读取了,楼主的代码中已经有这个了myds.ReadXml(strPath);
解决方案四:
XElementxe=XElement.Load(strPath);你这个不是在读取吗?
解决方案五:
自己动脑筋理解才深刻~~
解决方案六:
这个程序是别人编号的,他无法读取我想要的XML文件,有没有哪位大神能把这个程序给个注释,大概是什么意思,感激不尽