• XML


    XML的读取

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    
    namespace XMLRead
    {
        class Program
        {
            static void Main(string[] args)
            {
                string path = @"D:RuPengRupengXMLReadMyFirstXml.xml";
                XDocument xdoc = XDocument.Load(path);
                //根节点
                XElement root = xdoc.Root;
                //子节点
                //根节点下的所有的子节点
                foreach (XElement item in root.Elements())
                {
                    Console.WriteLine(item.Name+"=="+item.Attribute("StuId").Value);
                    foreach (XElement xele in item.Elements())
                    {
                        Console.WriteLine(xele.Name + "==" + xele.Value);
                    }
                }
    
                Console.ReadKey();
            }
        }
    }

    XML文件的写入:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    
    namespace XmlWrite
    {
        class Program
        {
            static void Main(string[] args)
            {
               // xml的写入
                XDocument xdoc = new XDocument();
                //根节点
                XElement root = new XElement("Person");
                XElement student = new XElement("Student");
                student.SetAttributeValue("StuId","12");
                student.SetElementValue("name","佐助");
                student.SetElementValue("gender", "");
                student.SetElementValue("age","15");
                root.Add(student);//student写入到Root
                xdoc.Add(root);//把根节点写入到文件中
                //保存
                xdoc.Save("1.xml");
            }
        }
    }

    将XML文件中的数据读取并写入数据库

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Data;
     4 using System.Data.SqlClient;
     5 using System.Linq;
     6 using System.Text;
     7 using System.Threading.Tasks;
     8 using System.Xml.Linq;
     9 
    10 namespace OutPutFromSql
    11 {
    12     class Program
    13     {
    14         static void Main(string[] args)
    15         {
    16             //读取xml中的数据写入数据库
    17             XDocument xdoc = XDocument.Load("student.xml");
    18             //根节点
    19             XElement root = xdoc.Root;
    20             foreach(XElement item in root.Elements())
    21             {
    22                 //Console.WriteLine(item.Element("Name").Value);
    23                 //Console.WriteLine(item.Element("Age").Value);
    24                 //Console.WriteLine(item.Element("Gender").Value);
    25                 string sql = "insert into  student(Sname,Sage,Ssex) values(@Name,@Age,@Gender)";
    26                 SqlParameter[] ps ={
    27                                new SqlParameter{ParameterName="@Name",Value= item.Element("Name").Value},
    28                               new SqlParameter{ParameterName="@Age",Value=Convert.ToInt32(item.Element("Age").Value)},
    29                                new SqlParameter{ParameterName="@Gender",Value=item.Element("Gender").Value}                                     
    30                                  };
    31                 SqlHelper.ExecuteNonQuery(sql,ps);
    32             }
    33 
    34             Console.WriteLine("结束");
    35             Console.ReadKey();
    36 
    37 
    38         }
    39 
    40         private static void ReaderData()
    41         {
    42             List<Student> list = new List<Student>();
    43             //读取数据
    44             DataTable dt = SqlHelper.GetDataTable("select * from student");
    45             if (dt.Rows.Count > 0)
    46             {
    47                 foreach (DataRow dr in dt.Rows)
    48                 {
    49                     Student stu = RowToStudent(dr);
    50                     list.Add(stu);
    51                 }
    52             }
    53             //遍历集合中的数据
    54             XDocument xdoc = new XDocument();
    55             XElement root = new XElement("Person");
    56             for (int i = 0; i < list.Count; i++)
    57             {
    58                 Student stu = list[i];
    59                 XElement student = new XElement("Student");
    60                 student.SetAttributeValue("Id", stu.Id.ToString());
    61                 student.SetElementValue("Name", stu.Sname);
    62                 student.SetElementValue("Age", stu.Sage.ToString());
    63                 student.SetElementValue("Gender", stu.Ssex);
    64                 root.Add(student);
    65             }
    66             xdoc.Add(root);
    67             xdoc.Save("student.Xml");
    68             //Console.ReadKey();
    69         }
    70 
    71         private static Student RowToStudent(DataRow dr)
    72         {
    73             Student stu = new Student();
    74             stu.Sage = Convert.ToInt32(dr["Sage"]);
    75             stu.Ssex = dr["Ssex"].ToString();
    76             stu.Id = Convert.ToInt32(dr["Id"]);
    77             stu.Sname = dr["Sname"].ToString();
    78             return stu;
    79         }
    80     }
    81 }
  • 相关阅读:
    jQuery学习笔记01
    webpack概念相关
    vue-cli4 配置公用scss样式的方法
    vue-cli4的路径别名
    webpack学习03——搭建本地服务器
    webpack学习02——Plungin的使用
    VSCode生成vue项目模板
    Vue的使用方法
    webpack学习01——初次打包
    文献随笔目录
  • 原文地址:https://www.cnblogs.com/ink-heart/p/5899981.html
Copyright © 2020-2023  润新知