开发者

Help moving from one xmlnode to another of the same name

开发者 https://www.devze.com 2023-03-18 05:33 出处:网络
I\'m trying to compare one attribute value to another, if it returns true i do nothing if false then i add some stuff.

I'm trying to compare one attribute value to another, if it returns true i do nothing if false then i add some stuff.

Her开发者_StackOverflow社区es the xml or part of it.

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

I'm using these methods to compare the data as shown by Jodrell.

In MXFDoc Class

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

In MXFProgramme class

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

        }
    }

This actually works quiet well, one thing i can't figure is why the returned DateTime values are out by 1 hour.

Thanks in advance.


The code below is fairly simple and readable.

There is almost certainly a more finessed way, perhaps with a Zip of staggerd enumerations.

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


Many thanks again Jodrell, with that piece of code, putting the rest togeather was much easier.

This is what i ended up with:

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

精彩评论

暂无评论...
验证码 换一张
取 消