从xml中删除重复的日期

时间:2014-01-07 12:09:39

标签: c# xml

我有一个xml文件,我需要删除那些在文件上有重复日期的节点,然后按时间顺序对它们进行排序。

  <?xml version="1.0"?>
    <Incidents xmlns:xsi="http://www.w3.org/2001/Date="2014-01-07" Time="09:54:00" FileName="2014-01-07_Iceland_-_Harrow_Middlesex.xml">
      <Incident>
        <Contract>18</Contract>
        <SerialNo>0000000001</SerialNo>
        <EntryTime>2013-12-31T00:13:23</EntryTime>
        <ExitTime>2013-12-31T01:00:47</ExitTime>
       <Registration>LL5AVP</Registration>
        <Location>Middlesex</Location>
      <Comment>Entry Date: Tuesday, 31 12 2013 on 00:13:23
    Exit Date: Tuesday, 31 12 2013 on 01:00:47</Comment>
    </Incident>
    <Incident>
    <Contract>18</Contract>
    <SerialNo>0000000002</SerialNo>
    <EntryTime>2013-12-31T00:13:23</EntryTime>
    <ExitTime>2013-12-31T01:00:47</ExitTime>
 <Registration>LL5AVP</Registration>
    <Location>Middlesex</Location>
<Comment>Entry Date: Tuesday, 31 12 2013 on 00:13:23 Exit Date: Tuesday, 31 12 2013 on 01:00:47</Comment><Evidence></Incident><Incident>
<Contract>18</Contract>
<SerialNo>0000000004</SerialNo>
<EntryTime>2013-12-31T10:48:56</EntryTime>
<ExitTime>2013-12-31T12:12:02</ExitTime>
<Registration>LY02XR</Registration>
<Location>Iceland - Harrow Middlesex</Location>
<Comment>Entry Date: Tuesday, 31 12 2013 on 10:48:56
    Exit Date: Tuesday, 31 12 2013 on 12:12:02</Comment>
<FileDescription>Exit</FileDescription>
</Evidence><Diplomatic>0</Diplomatic>
</Incident>

我有3个事件,其中2个评论同时<Comment>Entry Date: Tuesday, 31 12 2013 on 00:13:23我需要删除其中一个我是xml的新手,并尝试了这个但不是为了成功。

 private void button2_Click(object sender, EventArgs e)
        {
    XmlDocument xdoc1 = new XmlDocument();
                xdoc1.Load("C:\\Users\\IT-Administrator\\Desktop\\2014-01-07_Middlesex.xml");
                XmlNodeList Incidents = xdoc1.SelectNodes("//Incidents");
                List<string> IncidentsSeen = new List<string>();


                 foreach (XmlNode comments in Incidents)
                {
                   string  EntryDate  = Incidents.Attributes["Comments"].value;
                       if(IncidentsSeen.Contains(EntryDate))
                           Incidents.ParentNode.RemoveChild(Incidents);
                       else
                           IncidentsSeen.Add(EntryDate);
                 }
                Console.ReadKey(true);
            }
                }
                xdoc1.Save("X:\\Processed\\2013-07-06_Iceland_-_Spennymoorduplicate.xml");
            }

这是我的完全MXL代码::::

<?xml version="1.0"?>
<Incidents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Count="34" Date="2014-01-07" Time="09:54:00" FileName="2014-01-07_Middlesex..xml">
  <Incident>
    <Contract>18</Contract>
    <SerialNo>0000000001</SerialNo>
    <EntryTime>2013-12-31T00:13:23</EntryTime>
    <ExitTime>2013-12-31T01:00:47</ExitTime>
    <OffenceCode>94</OffenceCode>
    <Operator>P+D</Operator>
    <Registration>LS5VP</Registration>
    <Location> Middlesex</Location>
    <Make>N/A</Make>
    <Model>N/A</Model>
    <Colour>N/A</Colour>
    <Comment>Entry Date: Tuesday, 31 12 2013 on 00:13:23
Exit Date: Tuesday, 31 12 2013 on 01:00:47</Comment>
    <Nationality>-1</Nationality>
    <Foreign>-1</Foreign>
    <Evidence>
      <FileName>entry_143_LS5VP_2014-01-07_09-54-00.jpg</FileName>
      <FileDescription>Entry</FileDescription>
    </Evidence>
    <Evidence>
      <FileName>exit_143_LS5VP_2014-01-07_09-54-00.jpg</FileName>
      <FileDescription>Exit</FileDescription>
    </Evidence>
    <Diplomatic>0</Diplomatic>
  </Incident>
<Incident><Contract>18</Contract><SerialNo>0000000002</SerialNo><EntryTime>2013-12-31T00:13:23</EntryTime><ExitTime>2013-12-31T01:00:47</ExitTime><OffenceCode>94</OffenceCode><Operator>P+D</Operator><Registration>LS5VP</Registration><Location> Middlesex</Location><Make>N/A</Make><Model>N/A</Model><Colour>N/A</Colour><Comment>Entry Date: Tuesday, 31 12 2013 on 00:13:23
Exit Date: Tuesday, 31 12 2013 on 01:00:47</Comment><Nationality>-1</Nationality><Foreign>-1</Foreign><Evidence><FileName>entry_143_LS5VP_2014-01-07_09-55-30.jpg</FileName><FileDescription>Entry</FileDescription></Evidence><Evidence><FileName>exit_143_LS5VP_2014-01-07_09-55-30.jpg</FileName><FileDescription>Exit</FileDescription></Evidence><Diplomatic>0</Diplomatic></Incident><Incident><Contract>18</Contract><SerialNo>0000000003</SerialNo><EntryTime>2013-12-31T00:13:23</EntryTime><ExitTime>2013-12-31T01:00:47</ExitTime><OffenceCode>94</OffenceCode><Operator>P+D</Operator><Registration>LS5VP</Registration><Location> Middlesex</Location><Make>N/A</Make><Model>N/A</Model><Colour>N/A</Colour><Comment>Entry Date: Tuesday, 31 12 2013 on 00:13:23
Exit Date: Tuesday, 31 12 2013 on 01:00:47</Comment><Nationality>-1</Nationality><Foreign>-1</Foreign><Evidence><FileName>entry_143_LS5VP_2014-01-07_09-56-33.jpg</FileName><FileDescription>Entry</FileDescription></Evidence><Evidence><FileName>exit_143_LS5VP_2014-01-07_09-56-33.jpg</FileName><FileDescription>Exit</FileDescription></Evidence><Diplomatic>0</Diplomatic></Incident><Incident><Contract>18</Contract><SerialNo>0000000004</SerialNo><EntryTime>2013-12-31T10:48:56</EntryTime><ExitTime>2013-12-31T12:12:02</ExitTime><OffenceCode>94</OffenceCode><Operator>P+D</Operator><Registration>LY0XR</Registration><Location> Middlesex</Location><Make>N/A</Make><Model>N/A</Model><Colour>N/A</Colour><Comment>Entry Date: Tuesday, 31 12 2013 on 10:48:56
Exit Date: Tuesday, 31 12 2013 on 12:12:02</Comment><Nationality>-1</Nationality><Foreign>-1</Foreign><Evidence><FileName>entry_143_LY0XR_2014-01-07_09-57-22.jpg</FileName><FileDescription>Entry</FileDescription></Evidence><Evidence><FileName>exit_143_LY0XR_2014-01-07_09-57-22.jpg</FileName><FileDescription>Exit</FileDescription></Evidence><Diplomatic>0</Diplomatic></Incident><Incident><Contract>18</Contract><SerialNo>0000000032</SerialNo><EntryTime>2013-12-31T19:11:34</EntryTime><ExitTime>2013-12-31T19:36:17</ExitTime><OffenceCode>94</OffenceCode><Operator>P+D</Operator><Registration>AK1DX</Registration><Location> Middlesex</Location><Make>N/A</Make><Model>N/A</Model><Colour>N/A</Colour><Comment>Entry Date: Tuesday, 31 12 2013 on 19:11:34
Exit Date: Tuesday, 31 12 2013 on 19:36:17</Comment><Nationality>-1</Nationality><Foreign>-1</Foreign><Evidence><FileName>entry_143_AK1DX_2014-01-07_10-32-40.jpg</FileName><FileDescription>Entry</FileDescription></Evidence><Evidence><FileName>exit_143_AK1DX_2014-01-07_10-32-40.jpg</FileName><FileDescription>Exit</FileDescription></Evidence><Diplomatic>0</Diplomatic></Incident><Incident><Contract>18</Contract><SerialNo>0000000033</SerialNo><EntryTime>2013-12-31T19:11:34</EntryTime><ExitTime>2013-12-31T19:36:17</ExitTime><OffenceCode>94</OffenceCode><Operator>P+D</Operator><Registration>AK1DX</Registration><Location> Middlesex</Location><Make>N/A</Make><Model>N/A</Model><Colour>N/A</Colour><Comment>Entry Date: Tuesday, 31 12 2013 on 19:11:34
Exit Date: Tuesday, 31 12 2013 on 19:36:17</Comment><Nationality>-1</Nationality><Foreign>-1</Foreign><Evidence><FileName>entry_143_AK1DX_2014-01-07_10-33-29.jpg</FileName><FileDescription>Entry</FileDescription></Evidence><Evidence><FileName>exit_143_AK1DX_2014-01-07_10-33-29.jpg</FileName><FileDescription>Exit</FileDescription></Evidence><Diplomatic>0</Diplomatic></Incident><Incident><Contract>18</Contract><SerialNo>0000000034</SerialNo><EntryTime>2013-12-31T20:22:33</EntryTime><ExitTime>2013-12-31T22:57:48</ExitTime><OffenceCode>94</OffenceCode><Operator>P+D</Operator><Registration>MK0HL</Registration><Location> Middlesex</Location><Make>N/A</Make><Model>N/A</Model><Colour>N/A</Colour><Comment>Entry Date: Tuesday, 31 12 2013 on 20:22:33
Exit Date: Tuesday, 31 12 2013 on 22:57:48</Comment><Nationality>-1</Nationality><Foreign>-1</Foreign><Evidence><FileName>entry_143_MK0HL_2014-01-07_10-34-19.jpg</FileName><FileDescription>Entry</FileDescription></Evidence><Evidence><FileName>exit_143_MK0HL_2014-01-07_10-34-19.jpg</FileName><FileDescription>Exit</FileDescription></Evidence><Diplomatic>0</Diplomatic></Incident></Incidents>
Desired OUTPUT:::


     <?xml version="1.0"?>
<Incidents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Count="34" Date="2014-01-07" Time="09:54:00" FileName="2014-01-07_Middlesex..xml">
  <Incident>
    <Contract>18</Contract>
    <SerialNo>0000000001</SerialNo>
    <EntryTime>2013-12-31T00:13:23</EntryTime>
    <ExitTime>2013-12-31T01:00:47</ExitTime>
    <OffenceCode>94</OffenceCode>
    <Operator>P+D</Operator>
    <Registration>LS5VP</Registration>
    <Location> Middlesex</Location>
    <Make>N/A</Make>
    <Model>N/A</Model>
    <Colour>N/A</Colour>
    <Comment>Entry Date: Tuesday, 31 12 2013 on 00:13:23
Exit Date: Tuesday, 31 12 2013 on 01:00:47</Comment>
    <Nationality>-1</Nationality>
    <Foreign>-1</Foreign>
    <Evidence>
      <FileName>entry_143_LS5VP_2014-01-07_09-54-00.jpg</FileName>
      <FileDescription>Entry</FileDescription>
    </Evidence>
    <Evidence>
      <FileName>exit_143_LS5VP_2014-01-07_09-54-00.jpg</FileName>
      <FileDescription>Exit</FileDescription>
    </Evidence>
    <Diplomatic>0</Diplomatic>
  </Incident>
<Incident><Contract>18</Contract><SerialNo>0000000004</SerialNo><EntryTime>2013-12-31T10:48:56</EntryTime><ExitTime>2013-12-31T12:12:02</ExitTime><OffenceCode>94</OffenceCode><Operator>P+D</Operator><Registration>LY0XR</Registration><Location> Middlesex</Location><Make>N/A</Make><Model>N/A</Model><Colour>N/A</Colour><Comment>Entry Date: Tuesday, 31 12 2013 on 10:48:56
Exit Date: Tuesday, 31 12 2013 on 12:12:02</Comment><Nationality>-1</Nationality><Foreign>-1</Foreign><Evidence><FileName>entry_143_LY0XR_2014-01-07_09-57-22.jpg</FileName><FileDescription>Entry</FileDescription></Evidence><Evidence><FileName>exit_143_LY0XR_2014-01-07_09-57-22.jpg</FileName><FileDescription>Exit</FileDescription></Evidence><Diplomatic>0</Diplomatic></Incident><Incident><Contract>18</Contract><SerialNo>0000000032</SerialNo><EntryTime>2013-12-31T19:11:34</EntryTime><ExitTime>2013-12-31T19:36:17</ExitTime><OffenceCode>94</OffenceCode><Operator>P+D</Operator><Registration>AK1DX</Registration><Location> Middlesex</Location><Make>N/A</Make><Model>N/A</Model><Colour>N/A</Colour><Comment>Entry Date: Tuesday, 31 12 2013 on 19:11:34
Exit Date: Tuesday, 31 12 2013 on 19:36:17</Comment><Nationality>-1</Nationality><Foreign>-1</Foreign><Evidence><FileName>entry_143_AK1DX_2014-01-07_10-32-40.jpg</FileName><FileDescription>Entry</FileDescription></Evidence><Evidence><FileName>exit_143_AK1DX_2014-01-07_10-32-40.jpg</FileName><FileDescription>Exit</FileDescription></Evidence><Diplomatic>0</Diplomatic></Incident><Incident><Contract>18</Contract><SerialNo>0000000033</SerialNo><EntryTime>2013-12-31T19:11:34</EntryTime><ExitTime>2013-12-31T19:36:17</ExitTime><OffenceCode>94</OffenceCode><Operator>P+D</Operator><Registration>AK1DX</Registration><Location> Middlesex</Location><Make>N/A</Make><Model>N/A</Model><Colour>N/A</Colour><Comment>Entry Date: Tuesday, 31 12 2013 on 19:11:34
Exit Date: Tuesday, 31 12 2013 on 19:36:17</Comment><Nationality>-1</Nationality><Foreign>-1</Foreign><Evidence><FileName>entry_143_AK1DX_2014-01-07_10-33-29.jpg</FileName><FileDescription>Entry</FileDescription></Evidence><Evidence><FileName>exit_143_AK1DX_2014-01-07_10-33-29.jpg</FileName><FileDescription>Exit</FileDescription></Evidence><Diplomatic>0</Diplomatic></Incident><Incident><Contract>18</Contract><SerialNo>0000000034</SerialNo><EntryTime>2013-12-31T20:22:33</EntryTime><ExitTime>2013-12-31T22:57:48</ExitTime><OffenceCode>94</OffenceCode><Operator>P+D</Operator><Registration>MK0HL</Registration><Location> Middlesex</Location><Make>N/A</Make><Model>N/A</Model><Colour>N/A</Colour><Comment>Entry Date: Tuesday, 31 12 2013 on 20:22:33
Exit Date: Tuesday, 31 12 2013 on 22:57:48</Comment><Nationality>-1</Nationality><Foreign>-1</Foreign><Evidence><FileName>entry_143_MK0HL_2014-01-07_10-34-19.jpg</FileName><FileDescription>Entry</FileDescription></Evidence><Evidence><FileName>exit_143_MK0HL_2014-01-07_10-34-19.jpg</FileName><FileDescription>Exit</FileDescription></Evidence><Diplomatic>0</Diplomatic></Incident></Incidents>

这是我的代码:

 private void button1_Click(object sender, EventArgs e) // open file dialog works fine
        {

            Stream myStream;

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Filter = "XML files(.xml)|*.xml|all Files(*.*)|*.*";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = true;

            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    string strfilename = openFileDialog1.FileName;
                    string filetext = File.ReadAllText(strfilename);
                    richTextBox1.Text = filetext;
                    MessageBox.Show(strfilename);
                }


            }
        }


        private void button2_Click(object sender, EventArgs e)//Deleting  duplicate data
        {
            string test = richTextBox1.Text;
            TextReader tr = new StringReader(test);
            XDocument doc = XDocument.Load(tr);
            List<string> IncidentsSeen = new List<string>();
            foreach (var item in doc.Element("Incidents").Elements("Incident"))
{
    var EntryDate = item.Element("Comment").Value;
    if (IncidentsSeen.Contains(EntryDate))
    {
        item.Remove();
    }
    else
    {
        IncidentsSeen.Add(EntryDate);
    }
}



            //doc.Save(@"C:\Users\IT-Administrator\Desktop\2014-01-07_Middlesex.xml");

            //doc.Save(@"C:\Users\IT-Administrator\Desktop\22.xml");
        }



        private void button3_Click(object sender, EventArgs e)//saving..
        {

            //doc.Save(@"C:\Users\IT-Administrator\Desktop\22.xml");
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "XML files(.xml)|*.xml|all Files(*.*)|*.*";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
               using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
                using (StreamWriter sw = new StreamWriter(s))
                {
                    sw.Write(richTextBox1.Text);
                }
            }



        }

2 个答案:

答案 0 :(得分:2)

请尝试这个我已经包含了一个示例XML但是我没有包含名称空间。

<?xml version="1.0" encoding="utf-8" ?>
<Incidents>
  <Incident>
    <Contract>18</Contract>
    <SerialNo>0000000001</SerialNo>
    <EntryTime>2013-12-31T00:13:23</EntryTime>
    <ExitTime>2013-12-31T01:00:47</ExitTime>
    <Registration>LL5AVP</Registration>
    <Location>Middlesex</Location>
    <Comment>
      Entry Date: Tuesday, 31 12 2013 on 00:13:23
      Exit Date: Tuesday, 31 12 2013 on 01:00:47
    </Comment>
  </Incident>
  <Incident>
    <Contract>19</Contract>
    <SerialNo>0000000001</SerialNo>
    <EntryTime>2013-12-31T00:13:23</EntryTime>
    <ExitTime>2013-12-31T01:00:47</ExitTime>
    <Registration>LL5AVP</Registration>
    <Location>Middlesex</Location>
    <Comment>
      Entry Date: Tuesday, 31 12 2013 on 00:13:23
      Exit Date: Tuesday, 31 12 2013 on 01:00:47
    </Comment>
  </Incident>
  <Incident>
    <Contract>20</Contract>
    <SerialNo>0000000001</SerialNo>
    <EntryTime>2013-12-31T00:13:23</EntryTime>
    <ExitTime>2013-12-31T01:00:47</ExitTime>
    <Registration>LL5AVP</Registration>
    <Location>Middlesex</Location>
    <Comment>
      Entry Date: Tuesday, 31 12 2013 on 00:13:23
      Exit Date: Tuesday, 31 12 2013 on 01:00:47
    </Comment>
  </Incident>
  <Incident>
    <Contract>21</Contract>
    <SerialNo>0000000001</SerialNo>
    <EntryTime>2013-12-31T00:13:23</EntryTime>
    <ExitTime>2013-12-31T01:00:47</ExitTime>
    <Registration>LL5AVP</Registration>
    <Location>Middlesex</Location>
    <Comment>
      Entry Date: Tuesday, 31 12 2013 on 00:13:23
      Exit Date: Tuesday, 31 12 2013 on 01:00:47
    </Comment>
  </Incident>
</Incidents>

XDocument xdoc1 = XDocument.Load(@"D:\xxxx\Xxxxxxx\xxxx\TrialXML.xml");

xdoc1.Root.Elements("Incident") 
.OrderBy(d=>(Convert.ToDateTime(Convert.ToString(d.Element("EntryTime").Value)))) 
.GroupBy(s => (string)s.Element("Comment")) 
.SelectMany(g => g.Skip(1)) 
.Remove();

xdoc1.Save(@"D:\xxxx\Xxxxxxx\xxxx\TrialXML.xml");    

根据你的意见

加载多个Xml文件,您可以将它们合并为一个。

    var xml1 = XDocument.Load("file1.xml");
    var xml2 = XDocument.Load("file2.xml");

xml1.Descendants("Incident").LastOrDefault().AddAfterSelf(xml2.Descendants("Incident"‌​)); 
xml1.Save();

希望有所帮助 - Reference

更新根据您的评论,请尝试从RichTextBox加载XML。

 string test = richTextBox1.Text;
 TextReader tr = new StringReader(test);
 XDocument doc = XDocument.Load(tr);

答案 1 :(得分:1)

使用LINQ to XML:

var doc = XDocument.Load(@"C:\Users\IT-Administrator\Desktop\2014-01-07_Middlesex.xml");
List<string> IncidentsSeen = new List<string>();

foreach (var item in doc.Element("Incidents").Elements("Incident"))
{
    var EntryDate = item.Element("Comment").Value;
    if (IncidentsSeen.Contains(EntryDate))
    {
        item.Remove();
    }
    else
    {
        IncidentsSeen.Add(EntryDate);
    }
}

doc.Save(@"C:\Users\IT-Administrator\Desktop\2014-01-07_Middlesex.xml");

注意:EntryDate DateTime

修改

仅用于文件中的不同元素:

var doc = XDocument.Load(@"C:\Users\IT-Administrator\Desktop\2014-01-07_Middlesex.xml");

foreach (var itemsGroup in doc.Element("Incidents").Elements("Incident").GroupBy(x => x.Element("Comment").Value))
{
    foreach (var item in itemsGroup.Skip(1))
    {
        item.Remove();
    }
}

doc.Save(@"C:\Users\IT-Administrator\Desktop\2014-01-07_Middlesex.xml");