板牙  
失败是什么?没有什么,只是更走近成功一步;而成功是走过了所有通向失败的路...只要你心够决!

LINQ to XML程序集(System.Xml.Linq.dll)在3个不同的命名空间System.Xml.Linq、System.Xml.Schema和System.Xml.XPath。后两者只定义了少数类型。
System.Xml.Linq中有一套可控制的类型,如下:

成员 含义
XDocument 整个XML文档
XElement XML文档中的某个给定元素
XDeclaration XML文档开头的声明
XComment XML注释
XAttribute 代表一个特定XML元素的XML属性
XName、XNamespace 提供了一个简单的方式来定义和引用XML命名空间

引入Syst
em.Xml.Linq命名空间.
public class _Default 
    {
        /// <summary>
        /// XML样式结构
        /// </summary>
        public void CreateFunctionalXmlElement()
        {
            XElement inventory = new XElement("Inventory",
                new XElement("Car", new XAttribute("ID", "1"),
                    new XElement("Color", "Green"),
                    new XElement("PetName", "Stan")
                    )
                );
            //
            Label1.Text = inventory.ToString();
            //TextBox1.Text = inventory.ToString();//??????????????????????????????????????????????????
        }
        /// <summary>
        /// 创建XML文档
        /// </summary>
        void CreateFunctionalXmlDoc()
        {
            XDocument inventoryDoc =
                new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XComment("fffffffffff"),
                    new XElement("Inventory",
                        new XElement("Car", new XAttribute("ID", "1"),
                            new XElement("Color", "Green"),
                            new XElement("PetName", "Stan")
                            ),
                        new XElement("Car1", new XAttribute("ID1", "2"),
                            new XElement("Color", "red"),
                            new XElement("PetName", "haha")
                            )
                    )
               );
            string path = Server.MapPath(Request.ApplicationPath);
            inventoryDoc.Save(path+"dekunhaha.xml");
        }

        /// <summary>
        /// 从LINQ查询中生成文档
        /// </summary>
        public void CreateXmlDocFromArray()
        {
            //创建一个匿名类型数组
            var data = new[] {
                new{PetName="Melvin",ID=10},
                new{PetName="Pat",ID=11},
                };

            XElement vehicles =
                new XElement("Inventory",
                    from c in data
                        select new XElement("Car",
                            new XAttribute("ID",c.ID),
                            new XElement("PetName",c.PetName)
                            )
                    );
            Label1.Text = vehicles.ToString();
            /*匿名类型例子
             *
            product[] products=new product[2];
            var productQuery =
            from prod in products select new { prod.Color, prod.Price };
            foreach (var v in productQuery)
            {
            }
            */
        }
        /// <summary>
        /// 从字符串建立一个XElement
        /// </summary>
        public void LoadExistingXML()
        {
            string myElement = @"<Car id='3'><color>Yellow</color><make>Yugo</make></car>";
            XElement newXElement = XElement.Parse(myElement);
            //加载aa.xml
            XDocument myDoc = XDocument.Load("aa.xml");
        }

        /********************   aa.xml
         *
             <?xml version="1.0" encoding="utf-8" standalone="yes"?>
             <!--fffffffffff-->
             <Inventory>
               <Car ID="1">
                 <Make>Ford</Make>
                 <Color>Green</Color>
                 <PetName>Stan</PetName>
               </Car>
         *     <Car1 ID1="2">
                 <Make>VW</Make>
                 <Color>red</Color>
                 <PetName>haha</PetName>
               </Car1>
             </Inventory>
        */
        /// <summary>
        /// 选择某一个标签的固定值
        /// </summary>
        void PrintAllPetNames()
        {
            XElement xe = new XElement("aa.xml");
            var petNames = from pn in xe.Descendants("PetName") select pn.Value;            //Stan    haha
            foreach (var name in petNames)
            {
            }
            var fords = from c in xe.Descendants("Make") where "Ford" == c.Value select c;
        }


        void updateXML()
        {
            string path = Server.MapPath(Request.ApplicationPath);
            XElement xe =XElement.Load(path + "aa.xml");
            //xe.Add(new XElement("Car3", new XAttribute("ID1", "2"),
            //                new XElement("Color", "red"),
            //                new XElement("PetName", "haha")
            //                ));
            xe.FirstNode.AddBeforeSelf(new XElement("Car3", new XAttribute("ID1", "2"),
                            new XElement("Color", "red"),
                            new XElement("PetName", "haha")
                            ));
            var attr = xe.Elements("Car").Attributes("ID1");
            foreach (var id in attr)
            {
                string s = id.Value;
            }
            //xe.Save(path+"dekunhaha.xml");
        }


        protected void Button2_Click(object sender, EventArgs e)
        {
            //long arrayLength = (long)((4.0d) * (11/3.0d));
            //long i = arrayLength % 4;
            //if (arrayLength % 4 != 0)
            //{
            //    arrayLength += 4 - arrayLength % 4;
            //}
            //int base64ArraySize = (int)Math.Ceiling(10 / 3d) * 4;
            updateXML();
        }
    }

    class product
    {
       public string Color;
       public string Price;
    }

posted on 2009-08-24 15:31  板牙  阅读(328)  评论(0)    收藏  举报