[Implement] Read/Write XML File

[Implement] Read/Write XML File

Write XML File:


using System.Collections.Generic;
using System.Data;
using System.Text;

namespace ReadWrite_XML
{
    class Program
    {
        public static string[] SetUserInfo(int doType, object[][] data)
        {
            string[] result = new string[] { "success" };
            string file = "C:\\UserInfo.xml";
            DataSet ds = new DataSet();
            try
            {
                if (!System.IO.File.Exists(file))
                {
                    ds = CreateTableDS();
                    ds.WriteXml(file, XmlWriteMode.WriteSchema);
                }
                else
                {
                    ds.ReadXml(file);
                }

                DataRow[] drs;

                switch (doType)
                {
                    case 0: //insert
                        for (int i = 0; i < data.Length; i++)
                        {
                            DataRow row = ds.Tables[0].NewRow();
                            row.ItemArray = data[i];
                            ds.Tables[0].Rows.Add(row);
                        }

                        break;

                    case 1: //update
                        for (int i = 0; i < data.Length; i++)
                        {
                            drs = ds.Tables[0].Select("ID = '" + data[i][0].ToString() + "'");
                            drs[0]["UserName"] = data[i][1].ToString();
                            drs[0]["IsActive"] = data[i][2].ToString();
                            drs[0]["UpdateDate"] = data[i][3].ToString();
                        }

                        break;

                    case 2: //delete
                        for (int i = 0; i < data.Length; i++)
                        {
                            drs = ds.Tables[0].Select("ID = '" + data[i][0].ToString() + "'");
                            if (drs.Length > 0)
                                ds.Tables[0].Rows.Remove(drs[0]);

                            continue;
                        }

                        break;
                }
                ds.WriteXml(file, XmlWriteMode.WriteSchema);
            }
            catch (Exception ex)
            {
                result = new string[] { "error", ex.Message };
            }

            return result;
        }

        private static DataSet CreateTableDS()
        {
            DataSet ds = new DataSet();
            DataTable table = new DataTable("UserUnfo");
            DataColumn idColumn = new DataColumn();
            idColumn.DataType = System.Type.GetType("System.String");
            idColumn.ColumnName = "ID";
            table.Columns.Add(idColumn);

            DataColumn c0 = new DataColumn();
            c0.DataType = System.Type.GetType("System.String");
            c0.ColumnName = "UserName";
            table.Columns.Add(c0);

            DataColumn c1 = new DataColumn();
            c1.DataType = System.Type.GetType("System.Boolean");
            c1.ColumnName = "IsActive";
            table.Columns.Add(c1);

            DataColumn c2 = new DataColumn();
            c2.DataType = System.Type.GetType("System.DateTime");
            c2.ColumnName = "UpdateDate";
            table.Columns.Add(c2);

            DataColumn[] keys = new DataColumn[1];
            keys[0] = idColumn;
            table.PrimaryKey = keys;
            ds.Tables.Add(table);
            return ds;
        }

        static void Main(string[] args)
        {
            //insert
            SetUserInfo(0, new object[][]{
                new object[]{"a1", "nameA", true, DateTime.Now}});

            SetUserInfo(0, new object[][]{
                new object[]{"a2", "nameB", true, DateTime.Now}});

            //update
            //SetUserInfo(1, new object[][] {
            //    new object[] { "a1", "nameB", false, DateTime.Now } });

            //delete
            //SetUserInfo(2, new object[][] {
            //    new object[] { "a1" },
            //    new object[] { "a2" } });
        }
    }
}

Read XML File:

Method 1


{
    List<string> data = new List<string>();

    System.Xml.NameTable nt = new System.Xml.NameTable();
    System.Xml.XmlNamespaceManager nsmgr = new System.Xml.XmlNamespaceManager(nt);
    System.Xml.XmlParserContext context = new System.Xml.XmlParserContext(null, nsmgr, null, System.Xml.XmlSpace.None);
    System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(XMLStr, System.Xml.XmlNodeType.Element, context);

    while (reader.Read())
    {
        data.Add(reader.ReadString());
    }

    reader.Close();

    return data;
}

Method 2


{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml( XMLStr );
    XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName( nodeName ); // ex: nodeName = "DeptId"
    string result = xmlNodeList.Item(0).InnerText; // get the DeptId value

    return result;
}