Xml数据源为:

<?xml version="1.0" encoding="GB2312"?>
<Main>
  <Data>
    <username>admin</username>
    <time>2012-3-21 4:05:54</time>
    <ip>127.0.0.1</ip>
    <action>admin登录系统</action>
  </Data>
  <Data>
    <username>admin</username>
    <time>2012-3-21 4:19:20</time>
    <ip>127.0.0.1</ip>
    <action>admin删除了课件管理中id为11数据</action>
  </Data>
  <Data>
    <username>admin</username>
    <time>2012-3-21 4:20:10</time>
    <ip>127.0.0.1</ip>
    <action>admin在课件分类管理中id为0下添加技术类分类</action>
  </Data>
  <Data>
    <username>admin</username>
    <time>2012-3-21 4:20:37</time>
    <ip>127.0.0.1</ip>
    <action>admin在课件中课件分类id为48中添加技术一课件</action>
  </Data>
  <Data>
    <username>admin</username>
    <time>2012-3-21 4:26:39</time>
    <ip>127.0.0.1</ip>
    <action>admin登录系统</action>
  </Data>
  <Data>
    <username>admin</username>
    <time>2012-3-21 4:40:53</time>
    <ip>127.0.0.1</ip>
    <action>admin在课件中课件分类id为48中添加技术课程课件</action>
  </Data>
</Main>

查询节点UserName=admin的Action的节点值

   /// <summary>
        /// linq 查询Xml
        /// </summary>
        private static void linqToXml()
        {

            string file = File.ReadAllText("2012-03-21.xml", Encoding.Default);
            XElement element = XElement.Parse(file);
            var items = from book in element.Elements("Data")
                        where (string)book.Element("username") == "admin"
                        select book.Element("action");
            foreach (var item in items)
            {
                Console.WriteLine(item.Value);
            }
            Console.ReadKey();
        }

查询结果为:

admin登录系统
admin删除了课件管理中id为11数据
admin在课件分类管理中id为0下添加技术类分类
admin在课件中课件分类id为48中添加技术一课件
admin登录系统
admin在课件中课件分类id为48中添加技术课程课件

=====================================================================================

Xml操作

一:创建XML

    private static void linqToCreateXML()
        {
         

            XElement elment = 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")));
            Console.WriteLine(elment.ToString());
            Console.WriteLine("========================");
            //创建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(doc.ToString());
            Console.ReadLine();
        }

运行结果:

<Data>
  <aaa>
    <name>lily</name>
    <age>10</age>
  </aaa>
  <aaa>
    <name>jak</name>
    <age>110</age>
  </aaa>
</Data>
========================
<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>

==================================================================================

修改Xml指定节点的属性值

     /// <summary>
        /// 修改Xml属性值
        /// </summary>
        private static void LinqToUpdateAttrbut()
        {
            //创建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(doc.ToString());

         

            Console.WriteLine("===========2=============");
            //修改节点Name的属性,当属性值为11的时候,则修改为“我是修改后的”
            foreach (XAttribute attr in doc.Element("Data").Element("name").Attributes("type"))
            {
                if (attr.Value == "11")
                {
                    attr.Value = "我是修改后的";
                }
            }
            Console.WriteLine(doc.ToString());
            Console.WriteLine("=============2===========");
            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>
===========2=============
<Data>
  <aaa>
    <name>lily</name>
    <age>10</age>
  </aaa>
  <aaa>
    <name>jak</name>
    <age>110</age>
  </aaa>
  <name type="我是修改后的">asasas</name>
  <age>222</age>
</Data>
=============2===========

=====================================================================================

修改节点值

将name节点值为lily的age字段值修改为0(将lily的年龄修改为0岁)

 private static void LinqToUpdateValue()
        {
            //创建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(doc.ToString());
            //查询年龄为lily的节点
            var s1 = from s in doc.Element("Data").Elements("aaa") where s.Element("name").Value == "lily" select s;

            Console.WriteLine("===========2=============");
           //修改年龄节点为0
            foreach (var attr in s1)
            {
                attr.Element("age").SetValue("0");
          
            }
            Console.WriteLine(doc.ToString());
            Console.WriteLine("=============2===========");
            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>
===========2=============
<Data>
  <aaa>
    <name>lily</name>
    <age>0</age>
  </aaa>
  <aaa>
    <name>jak</name>
    <age>110</age>
  </aaa>
  <name type="11">asasas</name>
  <age>222</age>
</Data>
=============2===========

=========================================================================

为某节点设置属性1

SetAttributeValue(属性名, 属性值);

:当此节点没有属性的时候,则添加属性,当节点已经存在此数字的时候则修改属性

     private static void LinqToUpdateAttrbut1()
        {
            //创建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 XElement("age", "222")));


            Console.WriteLine(doc.ToString());



            Console.WriteLine("===========添加了属性============");
       
            //对指定节点添加属性
            doc.Element("Data").Element("aaa").Element("name").SetAttributeValue("aaaa", "ssssssss");
            Console.WriteLine(doc.ToString());
           ////////////////
            //文档更新触发事件
            doc.Changed += new EventHandler<XObjectChangeEventArgs>(doc_Changed);
            //////////////////

            //Console.WriteLine(doc.ToString());
            Console.WriteLine("===========我修改了属性属性============");
            //对指定节点更新属性
            doc.Element("Data").Element("aaa").Element("name").SetAttributeValue("aaaa", "ccccccc");

            Console.WriteLine(doc.ToString());
            Console.ReadLine();
        }
        static void doc_Changed(object sender, XObjectChangeEventArgs e)
        {
            Console.WriteLine("我是文档修改的事件,我抓住了,");
        }

 

运行结果为:

<Data>
  <aaa>
    <name>lily</name>
    <age>10</age>
  </aaa>
  <aaa>
    <name>jak</name>
    <age>110</age>
  </aaa>
  <name>
    <age>222</age>
  </name>
</Data>
===========添加了属性============
<Data>
  <aaa>
    <name aaaa="ssssssss">lily</name>
    <age>10</age>
  </aaa>
  <aaa>
    <name>jak</name>
    <age>110</age>
  </aaa>
  <name>
    <age>222</age>
  </name>
</Data>
===========我修改了属性属性============
我是文档修改的事件,我抓住了,
<Data>
  <aaa>
    <name aaaa="ccccccc">lily</name>
    <age>10</age>
  </aaa>
  <aaa>
    <name>jak</name>
    <age>110</age>
  </aaa>
  <name>
    <age>222</age>
  </name>
</Data>

 

 

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