如何知道xml文件中的更新属性

时间:2011-12-19 08:46:33

标签: c# asp.net xml database crud

问:

我允许我的用户上传xml文件以将其内容插入我的数据库。

我想是否有一些想法知道已更改的属性以更新数据库中的等效字段而不再次上载更新的文件(清除与该文件相关的所有数据并再次插入)。

是否有某种方式而非形式。

我的xml剪辑:

<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"/>

我的.cs剪辑:

                        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);

1 个答案:

答案 0 :(得分:1)

您可以将上传文件的InputStream(FileUpload#PostedFile)对象传递给XDocument.Load()方法(Linq-XML)以读取/解析XML文档。之后,您可以将xml数据与数据库进行比较。