案例1:获取指定节点的内容
public void XmlTest() { string xmlFileName=AppDomain.CurrentDomain.BaseDirectory+"\book.xml"; if (File.Exists(xmlFileName)) { XmlReader xmlReader = XmlReader.Create(xmlFileName); while (!xmlReader.EOF) //判断是否是流结尾 { if (xmlReader.MoveToContent() == XmlNodeType.Element && xmlReader.Name == "title") //MoveToContent()检测是否是内容节点,如果不是则后移下一个节点。 XmlNodeType.Element表示特征类型,特性(例如,id='123')。 { Console.WriteLine(xmlReader.ReadElementString());//获取当前节点的内容 } else { xmlReader.Read(); //Read()方法:循环下一个节点 } } } }
案例2:获取数据后数据转换使用ReadElementContentAsInt()、ReadElementContentAsDateTime()、ReadElementContentAsDouble()等方法,这样比先获取该值字符串再转换成其他数据类型的方法效率较高。
案例3:写入xml文件(会覆盖)
public void XmlWriteTest() { string xmlFileName = AppDomain.CurrentDomain.BaseDirectory + "\newBook.xml"; XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; //是否缩进 settings.NewLineOnAttributes = true; //是否将属性写入新行 XmlWriter writer = XmlWriter.Create(xmlFileName, settings); //静态方法实例化XmlWriter对象. XmlWriter会用新文件覆盖已有文件 writer.WriteStartDocument(); //声明xml为1.0的版本号 writer.WriteComment("这是一个注释"); //插入注释 writer.WriteStartElement("book"); //父节点名称 writer.WriteAttributeString("title", "New China"); //父节点的特征 writer.WriteAttributeString("author", "heyaoxing"); //父节点的特征 writer.WriteElementString("what", "I don't konw!"); //父节点的特征 writer.WriteStartElement("begin"); //子节点 writer.WriteElementString("beginItem", "asdasd"); //子节点下的内容节点和内容 writer.WriteEndElement(); //子节点的结束标签 writer.WriteElementString("name","234sdf"); //父节点的内容标签和内容 writer.WriteEndElement(); //父节点结束标签 writer.WriteEndDocument(); //声明结束文档 writer.Flush(); //将缓存数据写入流,同时清除当前缓存缓存数据 writer.Dispose(); //清除 witer对象 }
案例4:DataSet数据转换成Xml数据
public void DataSetToXmlTest() { string xmlFileName = AppDomain.CurrentDomain.BaseDirectory + "\book.xml"; string connecString = @"Data Source=(local);Initial Catalog=TestWeb1;User ID=ca;Password=123456"; string sqlString="SELECT TOP 1000 [USR_ID],[FAMILY_ID],[NAME],[RELATION] FROM [TestWeb1].[dbo].[USERS_FAMILY]"; using (SqlConnection con = new SqlConnection(connecString)) { SqlDataAdapter da = new SqlDataAdapter(sqlString, con); DataSet ds = new DataSet("USERS_FAMILY"); da.Fill(ds,"USERS_FAMILY"); ds.WriteXml(xmlFileName, XmlWriteMode.WriteSchema); //将当前内容以xml形式写入DataSet XmlDataDocument doc = new XmlDataDocument(ds); //将当前DataSet写入到Xml ds.Dispose(); da.Dispose(); } }
案例5:序列化和反序列化
public void XmlSerTest() { Project project = new Project(); project.name = "yaoxing"; project.age = 23; project.sex = "男"; project.address = "珠海市"; //序列化操作 TextWriter textWriter = new StreamWriter("project.xml"); //创建写入流对象 XmlSerializer sr = new XmlSerializer(typeof(Project)); //实例化XmlSerializer,同时XmlSerializer需要知道序列号的对象 sr.Serialize(textWriter, project); //执行序列化操作 textWriter.Dispose(); //反序列化 FileStream file = new FileStream("project.xml", FileMode.Open); //创建文件对象 XmlSerializer xsr = new XmlSerializer(typeof(Project)); Project pro = (Project)xsr.Deserialize(file); //执行反序列化操作 Console.WriteLine(pro.name); Console.WriteLine(pro.age); Console.WriteLine(pro.sex); Console.WriteLine(pro.address); }
案例6:XML To Linq
public void LinqToXmlTest() { string xmlFileName = AppDomain.CurrentDomain.BaseDirectory + "project.xml"; if (File.Exists(xmlFileName)) { XDocument xdoc = XDocument.Load(xmlFileName); //加载指定xml文件 Console.WriteLine("" + xdoc.LastNode.ToString()); //获取最后一个子节点的值 //Linq查询xml文档 var name = from a in xdoc.Descendants("name") //使用Linq查询方式1 select a.Value; Console.WriteLine(xdoc.Element("Project").Element("age").Value); //使用Linq查询方式2 Console.WriteLine(name.Count()); foreach (var item in name) { Console.WriteLine(item); } } XDocument xd = new XDocument(); XComment xc = new XComment("这是一条注释"); xd.Add(xc); //插入注释 XNamespace ns = "http://www.baidu.com/"; //定义命名空间 XElement xl = new XElement(ns + "company", //定义节点 new XAttribute("key","num1"), //定义特性 new XElement("partMent", //定义节点 new XElement("name", "online") //定义节点 ), new XComment("中间插入一条注释"), //插入注释 new XElement("sunlike", "name"), //定义节点 new XElement("sunlike", "name"), //定义节点 new XElement("sunlike", "name"), //定义节点 new XElement("sunlike", "name") //定义节点 ); xd.Add(xl); //定义节点载入文件 xd.Save("company.xml"); //保存xml }