简体   繁体   中英

How to know the updated attributes in an xml file

Q:

I allow my user to upload an xml file to insert its contents in my database.

I ask if there's some idea to know the attributes which have been changed to update the equivalent fields in my database without uploading the updated file again(clear all the data related to this file and insert it again).

Is there some way rather than a form .

My xml snip:

<day name="Saturday" short="Sa" day="0"/>
<day name="Sunday" short="Su" day="1"/>
<day name="Monday" short="Mo" day="2"/>
<day name="Tuesday" short="Tu" day="3"/>
<day name="Wednesday" short="We" day="4"/>
<day name="Thursday" short="Th" day="5"/>

My .cs snip :

                        List<Day> days = new List<Day>();//List of days.
                        IEnumerable<DayNode> resultDays = ReadDayNodes(targetFileName);
                        foreach (DayNode name in resultDays)
                        {
                            Day day = new Day();

                            if (!string.IsNullOrEmpty(ddl_batch.SelectedValue))
                                day.BatchNum = int.Parse(ddl_batch.SelectedValue);
                            if (!string.IsNullOrEmpty(ddl_department.SelectedValue))
                                day.DepCode = int.Parse(ddl_department.SelectedValue);
                            if (!string.IsNullOrEmpty(ddl_study.SelectedValue))
                                day.StudyCode = int.Parse(ddl_study.SelectedValue);
                            if (!string.IsNullOrEmpty(name.Day))
                                day.DayId = name.Day;
                            day.ShortName = name.Short;
                            day.Name = name.Name;
                            days.Add(day);
                        }
    /********************************************************************************************************************/
            private struct DayNode
            {
                public string Name { get; private set; }
                public string Short { get; private set; }
                public string Day { get; private set; }
                public DayNode(string Name, string Short, string Day)
                    : this()
                {
                    this.Name = Name;
                    this.Short = Short;
                    this.Day = Day;
                }
            }
        /********************************************************************************************************************/
        private static IEnumerable<DayNode> ReadDayNodes(string filePath)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            using (XmlReader xrdr = new XmlTextReader(fs))
                while (xrdr.Read())
                    if (xrdr.NodeType == XmlNodeType.Element && xrdr.LocalName == "day")
                        yield return new DayNode(xrdr.GetAttribute("name"), xrdr.GetAttribute("short"), xrdr.GetAttribute("day"));
        }
        /********************************************************************************************************************/
          resultDay = CommonUitilities.InsertDays(days);

You may pass InputStream (FileUpload#PostedFile) object of uploaded file to the XDocument.Load() method (Linq-XML) to read/parse XML document. After that you may compare xml data against the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM