ASP.NET XML与JSON

1, JS对HTML的操作是通过DOM来执行的,即JS通过DOM可以对HTML文档中的所有元素进行增删改查移,DOM给出了HTML文档中所有元素的访问入口,并提供了对这些元素进行增删改查移的方法和属性。

2,

ASP.NET XML与JSON
            //实例化一个XmlDocument
            XmlDocument doc = new XmlDocument();
            string fileName = Server.MapPath(@"~XMLXMLFile1.xml");
            //加载XmlDocument
            doc.Load(fileName);
            //得到文档的根节点
            XmlNode root = doc.DocumentElement;
            string info = "";
            foreach (XmlNode item in root.ChildNodes)
            {
                info += item.Attributes.Item(0).Value;
                info += item.Attributes.Item(1).Value;
                foreach (XmlNode items in item.ChildNodes)
                {
                    info += items.Value;
                }
                info += "<br/>";
            }
            Response.Write(info);
ASP.NET XML与JSON

3,

ASP.NET XML与JSON
 //创建XML文档对象
            XmlDocument doc = new XmlDocument();
            //创建XML文档描述
            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0","utf-8",null);

            //创建根节点
            XmlNode root = doc.CreateNode(XmlNodeType.Element,"classes",null);
            //创建子节点
            XmlNode class1 = doc.CreateNode(XmlNodeType.Element,"class1",null);
            XmlNode name = doc.CreateNode(XmlNodeType.Text,null,null);
            name.Value = "张三";

            XmlAttribute att = doc.CreateAttribute("stuno");
            att.Value = "007";
            XmlAttribute atts = doc.CreateAttribute("age");
            atts.Value = "88";

            //在根节点添加班级节点
            class1.AppendChild(name);
            class1.Attributes.Append(att);
            class1.Attributes.Append(atts);

            root.AppendChild(class1);
            //在文档中添加文档描述
            doc.AppendChild(declaration);
            //在文档中添加根节点
            doc.AppendChild(root);
            //保存文档
            string filename = Server.MapPath(@"~XMLclasssss.xml");
            doc.Save(filename);
ASP.NET XML与JSON

4,xml:本身是一个文本文件,用于存储数据的,有人把XML当成小型数据库来使用.

5.DOM树中节点:元素节点

        属性节点

        文本节点

        注释节点

        文档节点

6.

ASP.NET XML与JSON
//创建xml文档对象
            XmlDocument doc = new XmlDocument();
            //创建xml文档描述
            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            //创建根节点
            XmlNode root = doc.CreateNode(XmlNodeType.Element, "employee", null);

            List<students> list = new List<students> { new students() {name = "张三", age = "20" }, new students() {name = "李四", age = "88" } }; ;
            foreach (var item in list)
            {
                //创建子节点
                XmlNode xx = doc.CreateNode(XmlNodeType.Element, "student", null);
                XmlAttribute dd = doc.CreateAttribute("name");
                dd.Value = item.name;
                XmlAttribute ds = doc.CreateAttribute("age");
                ds.Value = item.age;
                xx.Attributes.Append(dd);
                xx.Attributes.Append(ds);
                root.AppendChild(xx);
            }
            doc.AppendChild(declaration);
            doc.AppendChild(root);

            string path = Server.MapPath(@"~/List/XMLFile1.xml");
            doc.Save(path);
ASP.NET XML与JSON

7,

ASP.NET XML与JSON
 //将dataset转换为list
            DataSet dd = Method();
            List<students> list = new List<students>();
            foreach (DataRow item in dd.Tables[0].Rows)
            {
                students stu = new students();
                stu.name = item["name"].ToString();
                list.Add(stu);
            }
            //将list转化为dataset
            foreach (var item in list)
            {
                DataSet da = new DataSet();
                DataTable dt = new DataTable();
                da.Tables.Add(dt);
                dt.Columns.Add("name");
                dt.Rows.Add(item.name);
            }
        }
        private DataSet Method()
        {
            string sql = "select * from students";
            SqlConnection con = new SqlConnection("server=.;database=stuDB;uid=sa;pwd=123456");
            SqlCommand cmd = new SqlCommand(sql,con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            return ds;
        }