Save List to XML file

后端 未结 6 1928
遥遥无期
遥遥无期 2021-02-05 13:15

I want to save records fetched from database in an XML file,
take x number of records from XML file into a custom collection List
process them and

6条回答
  •  执笔经年
    2021-02-05 13:44

    You Can save Your List into a DataTable then WriteXml

        T t0 = new T();
           t0.id=1;
           t0.property1="John";
           t0.property2="Doe";
        List Tlist = new List();
           Tlist.Add(t0);
    
        DataTable dt = new DataTable();
           dt.TableName = "People";
           dt.Columns.Add("ID");
           dt.Columns.Add("Name");
           dt.Columns.Add("Lastname");
    
    
            foreach(var item in tlist)
            {
                dt.Rows.Add();
                dt.Rows[dt.Rows.Count-1]["ID"] = item.name;
                dt.Rows[dt.Rows.Count - 1]["Name"] = item.id_cod;
                dt.Rows[dt.Rows.Count - 1]["Lastname"] = item.id_cod;
    
    
            }
            dt.WriteXml("test.Xml");
    

提交回复
热议问题