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

时间:2011-07-07 15:53:20

标签: c# xml xmlnode

我正在尝试将一个属性值与另一个属性值进行比较,如果它返回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小时。

提前致谢。

2 个答案:

答案 0 :(得分:0)

以下代码非常简单易读。

几乎可以肯定有一种更精细的方式,也许是一个错综复杂的列举。

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

答案 1 :(得分:0)

非常感谢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();
    }