zoukankan      html  css  js  c++  java
  • 参考文章C# Linq To XML的学习(创建并编辑XML树)示例

     
    2010-03-26 16:37
    Linq创建xml树,并对树中元素或属性的值修改。

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Xml.Linq;

    using System.Xml;

    namespace LinqToXMLStudy

    {

        //***********************************************************************************************************

        //Module:Program.cs

        //Author:factly

        //Create Date:2008-06-30

        //***********************************************************************************************************

        class Program

        {

            static void Main(string[] args)

            {

                //用linq创建XML树------------------------------------------------------------------------------------

                XElement xmlTree = new XElement("Contacts",

                                    new XElement("Contact",

                                        new XElement("Name", "Patrick Hines"),

                                        new XElement("Phone", "206-555-0144",

                                            new XAttribute("Type", "Home")),

                                        new XElement("Phone", "425-555-0145",

                                            new XAttribute("Type", "Work")),

                                        new XElement("Address",

                                            new XElement("Street1", "123 Main St"),

                                            new XElement("City", "Mercer Island"),

                                            new XElement("State", "WA"),

                                            new XElement("Postal", "68042")

                                         )

                                    )

                                );

                Console.WriteLine(xmlTree);

                //用linq创建XML树结束-------------------------------------------------------------------------------

                //修改xml中属性的值---------------------------------------------------------------------------------

                 IEnumerable<System.Xml.Linq.XElement> xe = xmlTree.Descendants("Phone");//查询出元素名为Phone的所有集合,查询时区分大小写

                 var v = xe.Where(p => p.Attribute("Type").Value == "Home");

               

                 foreach (var s in v)

                 {

                     s.SetValue("hello");

                 }

                 Console.WriteLine("修改后的结果为:");

                 Console.WriteLine(xmlTree);

                 //修改xml中属性的值结束-----------------------------------------------------------------------------

                Console.ReadKey();

            }

        }

    }

    http://hi.baidu.com/xbs729/blog/item/30de2c266839b627d407429c.html

  • 相关阅读:
    高并发时,使用Redis应注意的问题 及 Redis缓存帮助类
    NetCore3.1 如何添加带有JWT Token 验证的Swagger
    CSS 技巧一则 -- 不定宽溢出文本适配滚动
    ROS costmap_2d局部障碍物无法清除和机器人到点摇摆
    ROS OccupancyGrid使用说明
    ROS RVIZ显示点云地图的二维投影
    Linux 文档生成器doxygen
    高翔博士 资源索引
    SLAM中的数学基础 第四篇 李群与李代数2
    shell(8):循环
  • 原文地址:https://www.cnblogs.com/angestudy/p/2002765.html
Copyright © 2011-2022 走看看