删除节点名为lily的节点

     private static void LinqTodelete()
        {
           
            //创建Xml
            XDocument doc = new XDocument(new XElement("Data",
                new XElement("aaa",
                    new XElement("name", "lily"),
                    new XElement("age", "10")),
                new XElement("aaa",
                    new XElement("name", "jak"),
                    new XElement("age", "110"))));

            //为Xml之追加节点,并且设置属性
            doc.Element("Data").Add(
                new XElement("name",
                    new XAttribute("type", "11"), "asasas"),
                    new XElement("age", "222"));

            Console.WriteLine("===========删除前============");
            Console.WriteLine(doc.ToString());
            //删除节点值为lily 的节点
            var s1 = from s in doc.Element("Data").Elements("aaa") where s.Element("name").Value == "lily" select s;

            Console.WriteLine("===========删除后============");
  
            s1.Remove();
            doc.Add(s1);
            Console.WriteLine(doc.ToString());
       
            Console.ReadLine();
        }

 

运行结果

===========删除前============
<Data>
  <aaa>
    <name>lily</name>
    <age>10</age>
  </aaa>
  <aaa>
    <name>jak</name>
    <age>110</age>
  </aaa>
  <name type="11">asasas</name>
  <age>222</age>
</Data>
===========删除后============
<Data>
  <aaa>
    <name>jak</name>
    <age>110</age>
  </aaa>
  <name type="11">asasas</name>
  <age>222</age>
</Data>

 

 

posted on 2012-04-16 15:22  高兴happy  阅读(665)  评论(0编辑  收藏  举报