学习XML.LINQ实战经验

XElement val = new XElement("Value",

                                                new XAttribute("ID", "1243"),

                                                new XAttribute("Type", "int"),

                                                new XAttribute("ConvertableTo", "double"),

                                        "100");

            string ss = val.Name.ToString();

            Console.WriteLine(ss);

            Console.WriteLine("***********************************************************");

 

         

            XElement person = new XElement("人类", "我是李新海 ");

            XAttribute personXattrSex = new XAttribute("sex", "");

            XAttribute personXattrAge = new XAttribute("年龄", "int");

            XAttribute personXattrLike = new XAttribute("爱好", "吃饭");

            person.Add(personXattrSex, personXattrAge, personXattrLike);

 

            XElement Animal = new XElement("动物类B", "我叫做小狼狗");

            XAttribute AnimalXattr= new XAttribute("主人", "未知");

            Animal.Add(AnimalXattr);

 

            XElement person2 = new XElement("人类", "我是张三");

            XAttribute person2XattrSex = new XAttribute("sex", "");

            XAttribute person2XattrAge = new XAttribute("年龄", "int");

            XAttribute person2XattrLike = new XAttribute("爱好", "type");

            person2.Add(person2XattrSex, person2XattrAge, personXattrLike);

         

            XElement person3 = new XElement("人类", "我是 李四");

            XAttribute person3XattrSex = new XAttribute("sex", "");

            XAttribute person3XattrAge = new XAttribute("年龄", "int");

            XAttribute person3XattrLike = new XAttribute("爱好", "type");

            person3.Add(person3XattrSex, person2XattrAge, personXattrLike);

 

            XElement Animal1 = new XElement("动物类C", "我是李新海的小狗");

            XAttribute Anima1lXattr = new XAttribute("主人", "李新海");

            Animal1.Add(Anima1lXattr);

            person.Add(Animal1);

 

            XElement Animal2= new XElement("动物类D", "我是李新海的小狗的儿子");

            XAttribute Anima2lXattr = new XAttribute("宠物基因", "动物类");

            Animal2.Add(Anima2lXattr);

 

            Animal1.Add(Animal2);

 

            Console.WriteLine("***********************************************************");

 

 //添加关系

            val.Add(person);

            val.Add(person2);

            val.Add(Animal);

            val.Add(person3);

            doc.Add(val);

            person2.AddAfterSelf(new XElement("人类", "我是追加(AddAfterSelf)上的人类"));

            val.Save("d://aa.xml");

           

            //属性相关的

            IEnumerable<XAttribute> xa = from att in val.Attributes() //其中val是全局的根节点

                                         select att;

            foreach (XAttribute a in xa)

            {

                Console.Write(" 输出根节点的所有的属性的LOCAlNAME名字!:");

                Console.WriteLine(a.Name.LocalName);

 

                Console.Write(" 输出根节点的所有的属性Name名字:");

                Console.WriteLine(a.Name);

 

                Console.Write(" 输出各个属性的NodeType:");

                Console.WriteLine(a.NodeType);

 

                //Console.WriteLine(" 输出各个属性的Parent:");

                //Console.Write(a.Parent);

                a.Parent.Add(new XElement("我这个人类好奇怪啊","我是在某一些属性的attribute.Parent方法上又ADD"));//不知道为什么没有文件中显示呢!

 

 

                if (a.Name.LocalName == "ID")

                {

                    Console.WriteLine(" 找到ID的属性了!好高兴啊:");

                    Console.WriteLine("输出一下找到的值"+a.Value);

                }

            }

 

            Console.WriteLine("***********************************************************");

 

            //属性一层,挖掘了第一层!!仅仅是第一层

 

            Console.WriteLine(" #####属性al.Elements().Attributes()间接得到的########");

            Console.WriteLine(" 挖掘了第一层!!仅仅是第一层");

            IEnumerable<XAttribute> atrrb= from att in val.Elements().Attributes()//其中val是全局的根节点

                                         select att;

            foreach (XAttribute a in atrrb)

            {

                Console.Write(" 输出根节点Elements()..Attributes()的所有的属性的LOCAlNAME名字!:");

                Console.WriteLine(a.Name.LocalName);

 

                Console.Write(" 输出根节点Elements()..Attributes()的所有的属性Name名字:");

                Console.WriteLine(a.Name);

 

                Console.Write(" 输出各Elements()..Attributes()个属性的NodeType:");

                Console.WriteLine(a.NodeType);

 

                //Console.WriteLine(" 输出各个属性的Parent:");

                //Console.Write(a.Parent);

               

                if (a.Name.LocalName == "ID")

                {

                    Console.WriteLine(" 找到Elements()ID..Attributes()的属性了!好高兴啊:");

                    Console.WriteLine("输出Elements()..Attributes()一下找到的值" + a.Value);

                }

            }

 

 

 

            Console.WriteLine("***********************************************************");

 

          

            IEnumerable<XAttribute> xar = from att in val.Attributes()

                                         select att;

            foreach (XAttribute a in xar)

            {

            

                    Console.WriteLine("属性节点的类型是(Atti.Name):" + a.Name + "属性节点的值(atti.Value):" + a.Value);

            }

            Console.WriteLine("***********************************************************");

            Console.WriteLine("检索<人类>标签的元素并且是子一级的元素中属性标签<主人>的值是<>李新海de ");

            XElement allelement = XElement.Load("d://aa.xml");

 

            IEnumerable<XElement> childElements = from el in allelement.Elements("人类").Elements()

                                                  where (string)el.Attribute("主人") == "李新海"

                                                  select el;

            foreach (XElement xet in childElements)

            {

                string strty = xet.GetType().ToString();

                string strs = xet.Value;

                Console.WriteLine("元素的类型是:"+strty+"元素的值是:"+strs);

                //Console.WriteLine();

            }

            Console.WriteLine("结果证明居然给把此标签下的所有的vallue给合并到一行去了");

 

            Console.WriteLine("***********************************************************");

 

 

 

            Console.WriteLine("实验证明 allelement.Elements()是没有子类的,但是却把值给合并了");

            //Process mPro = new Process();

            //string path = System.Environment.GetFolderPath(Environment.SpecialFolder.System);

            //mPro.StartInfo.FileName = "d://aa.xml";

            //mPro.Start();

            IEnumerable<XElement> de =

            from el in allelement.Elements()

            select el;

            foreach (XElement el in de)

            {

                Console.WriteLine("el.Name" + el.Name);//没有子类的动物类了

                Console.WriteLine("el.Value" + el.Value);

            }

             Console.WriteLine("_________________________________________");

            Console.WriteLine("_________________________________________");

            Console.WriteLine("_________________________________________");

            Console.WriteLine("_________________________________________");

 

            Console.WriteLine("实验证明 allelement.Descendants()是输出了全部的子类,证明这个比较完善");

            IEnumerable<XElement> ide =

       from el in allelement.Descendants()

          select el;

            foreach (XElement el in ide)

            {

                Console.WriteLine("el.Name"+el.Name);//全部输出

                Console.WriteLine("el.Value"+el.Value);

            }

 

            Console.WriteLine("***********************************************************");

 

            XElement cust = new XElement("PhoneNumbers",

                             new XElement("Phone",

                             new XAttribute("type", "home"), "555-555-5555"),     new XElement("Phone",    new XAttribute("type", "work"),  "555-555-6666")

             );

            IEnumerable<XElement> elList = from el in cust.Descendants("Phone")

                                           select el;

            foreach (XElement el in elList)

                Console.WriteLine((string)el.Attribute("type"));//(string)el.Attribute("type").Valuse效果相同

 

 

 

 

 

 

        }

    }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值