天天看点

linq to xml

一      private static string name, num;

        public static string num

        {

            get { return sysconfig.num; }

            set { sysconfig.num = value; }

        }

        public static string name

            get { return sysconfig.name; }

            set { sysconfig.name = value; }

        }        

        public static void createxml(string filepath)                

           studentdatacontext studc = new studentdatacontext();

           var stu = from s in studc.student

                      select s;

            xelement stuxml = new xelement("students");

            foreach (student s in stu)

            {

                stuxml.add(new xelement("student",

                           new xattribute("name", s.stuname),

                           new xelement("number", s.stunum)));

            }

            stuxml.save(filepath);

        public static  xdocument doc;

        public static void readdefaultvalue(string filepath) 

            // 有很多student标签,默认为第一个

            try

                doc = xdocument.load(filepath);

                num = doc.element("students").element("student").value;

                name = doc.element("students").element("student").attribute("name").value;

            catch

                // 如果try里有错,也不影响运行

                num = "100000";

                name = "xy";

        public static ienumerable<xelement> find(string filepath)

             doc = xdocument.load(filepath);

             var result = from r in doc.element("students").elements("student")

                          where (string)r.attribute("name").value == "xy"

                          select r;

            return result;

        public static void remove(string filepath)

            doc = xdocument.load(filepath);

            doc.element("students").elements("student").where(ss => ss.attribute

           ("name").value.tostring() == "xy").remove();

            doc.save(filepath);

        public static void add(string filepath)

            doc.element("students").add(new xelement("student",

                                        new xattribute("name", "小明"),

                                        new xelement("number", "111")));

        public static void setvalue(string filepath)

            doc.element("students").elements("student").where(ee => (string)ee.attribute("name")

             == "xy").firstordefault().setattributevalue("name", "xy");

            == "xy").firstordefault().setelementvalue("number", "100000");

         }

    }

二.

public static class blackwhite

    {

        //private static string findconfigfile(string filename)

        //{

        //    //return httpcontext.current.server.mappath(string.format("~/config/{0}",

              filename));  // 找到xml文件路径

        //}

        private static ienumerable<string> black;

        public static ienumerable<string> black1

            get { return blackwhite.black; }

            set { blackwhite.black = value; }

        private static ienumerable<string> white;

        public static ienumerable<string> white1

          get { return blackwhite.white; }

          set { blackwhite.white = value; }

        public static void usexml()

            //xdocument doc = xdocument.load(findconfigfile("blackwhite.xml"));

            string str = system.environment.currentdirectory;

            xdocument doc = xdocument.load(string.format(@"{0}\blackwhite.xml", str));

            black = doc.element("list").element("black").elements("name").select(element =>

                    element.value);

            white = doc.element("list").element("white").elements("name").select(element =>

            process[] processes = process.getprocesses();

            foreach (process p in processes)

                if (black.contains(p.processname))

                {

                    p.kill();

                    messagebox.show("上课请不要打开无关程序");

                }

}

<?xml version="1.0" encoding="utf-8" ?>

<list>

  <black>

   <name>qq</name>

   <name>aliim</name>

  </black>

   <white>

     <name>severudp</name>

     <name>clientudp</name>

   </white>

</list>