linq to xml

  public class Toxml {
        public static void CreateDocument() {
            //路径
            string path = @"e:";
            //XDeclaration xml的声明
            //XElement xml的一个节点 
            XDocument xdoc = new XDocument(new XDeclaration("2.0", "utf-8", "yes"),
                                           new XElement("Root",11111));
            xdoc.Save(path);
        }

        public static void CreateCategories() {
            string path = @"e:";
            XElement root = new XElement("Categories",
                new XElement("Category", new XElement("CategoryID", Guid.NewGuid(), new XElement("CategoryName", "王俊"))
                ),
                new XElement("Category", new XElement("CategoryID", Guid.NewGuid()), new XElement("CategoryName", "张嵩云")));
            root.Save(path);
        }

        public static void CreateCategoriesByXAttribute() {
            string path = @"e:";
            XElement root = new XElement("Categories",
                 new XAttribute("CategoryId",Guid.NewGuid()), new XElement("CategoryName", "王俊"),
                 new XElement("Category", new XElement("CategoryID", Guid.NewGuid()), new XElement("CategoryName", "张嵩云")));
            XAttribute attribute = root.Element("Categories").Attribute("CategoryId");
            attribute.Remove();
            root.Save(path);

        }

        public static void CreateXDocument() {
            string path = @"e:";
            XDocument xdoc = new XDocument(
                //处理指令
                new XProcessingInstruction("xml-stylesheet", "title='EmpInfo'"),
                //注解
                new XComment("这是xml注释"),
                new XElement("Root",
                    new XElement("Employees"
                    ,new XAttribute("id",1)
                    ,new XElement("Name","刘海洋"))
                ),
                //注解
                new XComment("这块是xml是结束的注释")
                );
       xdoc.Element("Root").Add(new XAttribute("AddDate", DateTime.Now.ToShortDateString()));
            xdoc.Save(path);
        }

        //加载已有的xml
        public static void LoadFromFile() {
            XElement xml = XElement.Load("");
            Console.WriteLine(xml.ToString());
        }

 

学习了大概就这些东西咯

posted @ 2016-05-23 15:23  海_洋  阅读(83)  评论(0)    收藏  举报