帮助从一个xmlnode移动到另一个同名的xmlnode

本文关键字:xmlnode 移动 另一个 一个 帮助 | 更新日期: 2023-09-27 18:01:23

我试图比较一个属性值到另一个,如果它返回true我什么都不做,如果false然后我添加一些东西。

这是xml或它的一部分。

<tv grabber-info-name="MCE7-digiguide">
<channel id="101" id2="817">
<display-name>BBC 1 West</display-name>
</channel>
<programme channel="101" channel2="817" start="20110626013000 +0000" stop="20110626060000 +0000">
</programme>
<channel id="102" id2="5223">
<display-name>BBC Two</display-name>
</channel>
<programme channel="102" channel2="5223" start="20110626021000 +0000" stop="20110626220000 +0000">
</programme>
</tv>

我正在使用这些方法来比较Jodrell所显示的数据。

MXFDoc类

 public void CheckTimes()
    {
        MXFProgramme previousProg = null;
        MXFProgramme nextProg = null;
        foreach (MXFProgramme prog in Programmes.OrderBy(p => p.channel))
        {
            nextProg = prog;
            if (previousProg != null)
            {
                if (previousProg.stopped != nextProg.started)
                {
                    MessageBox.Show(previousProg.stopped +"  "+ nextProg.started.ToString() +"  "+ previousProg.channel +"  "+ nextProg.channel);         
                }
            }
            previousProg = prog;
        }
    } 

在MXFProgramme类

public DateTime stopped
    {
        get
        {
            string StopString = xProgramme.Attributes.GetNamedItem("stop").Value;
            DateTime time = new DateTime(int.Parse(StopString.Substring(0, 4)), int.Parse(StopString.Substring(4, 2)), int.Parse(StopString.Substring(6, 2)), int.Parse(StopString.Substring(8, 2)), int.Parse(StopString.Substring(10, 2)), int.Parse(StopString.Substring(12, 2)));
            return time.ToUniversalTime();
        }
    }
    public DateTime started
    {
        get
        {
            string StartString = xProgramme.Attributes.GetNamedItem("start").Value;
            DateTime time = new DateTime(int.Parse(StartString.Substring(0, 4)), int.Parse(StartString.Substring(4, 2)), int.Parse(StartString.Substring(6, 2)), int.Parse(StartString.Substring(8, 2)), int.Parse(StartString.Substring(10, 2)), int.Parse(StartString.Substring(12, 2)));
            return time.ToUniversalTime();
        }
    }

这实际上工作得很好,我不能弄清楚的一件事是为什么返回的DateTime值是1小时。

帮助从一个xmlnode移动到另一个同名的xmlnode

下面的代码相当简单和可读。

几乎肯定有一种更巧妙的方法,也许是交错枚举的Zip。

public void CheckTimes
{
    MFXProgramme previousProg = null;
    MFXProgramme nextProg = null;
    foreach (MFXProgramme prog in Programmes.OrderBy(p => p.Start))
    {
        nextProg = prog;
        if (previous != null)
        {
            if (previous.Stop != next.Start)
            {
                //Gap
            }
        }
        previousProg = prog;
    }
}    
class MXFProgramme
{
    ...
    public DateTime Start
    {
        get
        {
            return DateTime.Parse(xProgramme.Attributes["start"].Value);
        }
    }
    public DateTime Stop
    {
        get
        {
            return DateTime.Parse(xProgramme.Attributes["stop"].Value);
        }
    }
}

再次感谢Jodrell,有了这段代码,把剩下的代码放在一起就容易多了。

这是我最后的结果:

public void CheckTimes()
    {
        MXFProgramme previousProg = null;
        MXFProgramme nextProg = null;
        using (XmlTextWriter writer = new XmlTextWriter("Gaps.xml",Encoding.UTF8))
        {
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("tv");
            foreach (var prog in Programmes.OrderBy(p => p.channel))
            {
                nextProg = prog;
                if (previousProg != null)
                {
                    if (previousProg.stopped != nextProg.started)
                    {
                        if (previousProg.channel == nextProg.channel)
                        {
                            writer.WriteStartElement("programme");
                            writer.WriteAttributeString("channel", previousProg.channel);
                            writer.WriteAttributeString("channel2", previousProg.channel2);
                            writer.WriteAttributeString("start", string.Format("{0:yyyyMMddHHmmss} +0000", previousProg.stopped.ToLocalTime()));
                            writer.WriteAttributeString("stop", string.Format("{0:yyyyMMddHHmmss} +0000", nextProg.started.ToLocalTime()));
                            writer.WriteStartElement("title");
                            writer.WriteString("Channel Off Air");
                            writer.WriteEndElement();
                            writer.WriteStartElement("desc");
                            writer.WriteString("Programmes start again at" + " : " + nextProg.started.ToLocalTime().ToShortTimeString());
                            writer.WriteEndElement();
                            writer.WriteStartElement("category");
                            writer.WriteString("General");
                            writer.WriteEndElement();
                            writer.WriteStartElement("length");
                            writer.WriteAttributeString("units", "minutes");
                            writer.WriteString(nextProg.started.Subtract(previousProg.stopped).TotalMinutes.ToString());
                            writer.WriteEndElement();
                            writer.WriteEndElement();
                        }
                    }
                }
                    previousProg = prog;
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            StartMXF();
        }
    }
    public void StartMXF()
    {
        XmlDocument xXMLTVDoc = new XmlDocument();
        xXMLTVDoc.Load("Gaps.xml");
        XmlNodeList xNodes = xXMLTVDoc.SelectNodes("//tv");

        foreach (XmlNode xNode in xNodes)
        {
            foreach (XmlNode xN in xNode.ChildNodes)
            {
                if (xN.Name == "programme")
                {
                    MXFProgramme newProg = new MXFProgramme(xN);
                    Programmes.Add(newProg);
                    foreach (MXFChannel Chn in Channels)
                    {
                        if (Chn.id == newProg.channel)
                            Chn.Programmes.Add(newProg); 
                    }
                    if (!SeriesList.Contains(newProg.Series))
                        SeriesList.Add(newProg.Series);

                }
            }

        }
        Programmes.Sort(new ArrayListSort());
        WriteMXF();
    }