码迷,mamicode.com
首页 > 其他好文 > 详细

linq to xml

时间:2014-12-22 22:42:04      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

  1 using UnityEngine;
  2 using System;
  3 using System.Collections;
  4 using System.Collections.Generic;
  5 using DG.Tweening;
  6 using System.Linq;
  7 using System.Xml.Linq;
  8 
  9 public class MyTest2 : MonoBehaviour
 10 {
 11 
 12     string Path { get; set; }
 13     string NewPath { get; set; }
 14 
 15     void Awake()
 16     {
 17         Path = Application.persistentDataPath;
 18         CreateXml();
 19     }
 20 
 21     void OnGUI()
 22     {
 23         //if (GUI.Button(new Rect(10, 10, 150, 100), "CreateXml")) CreateXml();
 24         if (GUI.Button(new Rect(10, 150, 150, 100), "RemoveElement1")) RemoveElement1();
 25         if (GUI.Button(new Rect(10, 320, 150, 100), "AddBeforeElement2")) AddBeforeElement2();
 26     }
 27 
 28 
 29     void CreateXml()
 30     {
 31         XElement srcTree = new XElement("Root",
 32                 new XElement("Element1", 1,
 33                     new XElement("Child1", 1),
 34                     new XElement("Child2", 2),
 35                     new XElement("Child3", 3),
 36                     new XElement("Child4", 4),
 37                     new XElement("Child5", 5)),
 38                 new XElement("Element2", 2,
 39                     new XElement("Child1", 1),
 40                     new XElement("Child2", 2),
 41                     new XElement("Child3", 3),
 42                     new XElement("Child4", 4),
 43                     new XElement("Child5", 5)),
 44                 new XElement("Element3", 3,
 45                     new XElement("Item", 1),
 46                     new XElement("Item", 2),
 47                     new XElement("Item", 3),
 48                     new XElement("Item", 4),
 49                     new XElement("Item", 5))
 50             );
 51 
 52         NewPath = Path + "/" + srcTree.Name + ".xml";
 53         srcTree.Save(NewPath);
 54 
 55         //XElement newTree = XElement.Load(NewPath);
 56         //foreach (var item in srcTree.Elements())
 57         //{
 58         //    //Element1 : 112345
 59         //    Debug.Log(item.Name + " : " + item.Value);
 60         //    foreach (var item1 in item.Elements())
 61         //    {
 62         //        //Child1 : 1
 63         //        //Child1 : 2
 64         //        //Child1 : 3
 65         //        //Child1 : 4
 66         //        //Child1 : 5
 67         //        Debug.Log(item1.Name + " : " + item1.Value);
 68         //    }
 69         //}
 70 
 71         
 72     }
 73 
 74     void RemoveElement1()
 75     {
 76         XElement srcTree = XElement.Load(NewPath);
 77 
 78         var select = srcTree.Elements().Where(t => t.Name == "Element1").ToList();
 79         
 80         Debug.Log("select.Count" + select.Count);
 81         ///要 转化 成 list
 82         foreach (var item in select)
 83         {
 84             item.Remove();
 85         }
 86 
 87         NewPath = Path + "/" + srcTree.Name + ".xml";
 88         srcTree.Save(NewPath);
 89     }
 90 
 91     void AddBeforeElement2()
 92     {
 93         XElement srcTree = XElement.Load(NewPath);
 94 
 95         var select = srcTree.Element("Element2");
 96 
 97         select.AddBeforeSelf(new XElement("Element1","1"));
 98 
 99         NewPath = Path + "/" + srcTree.Name + ".xml";
100         srcTree.Save(NewPath);
101     }
102 }

 

linq to xml

标签:

原文地址:http://www.cnblogs.com/chongxin/p/4178984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!