eliminating duplicate attributes

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I wanted to post here to see if there is a better way of doing this.
I have a few attributes below that are the same value...I want to grab the first of those values only..
This parses the NCAA scores from various networks. the xml may or may not list the same network twice or 3 times.. I only need the first...
I have the following XML sample:
<div style="background-color:white; color:black
<pre>- <span style="color:blue <<span style="color:#a31515 mmodAdWidget <span style="color:red timestamp<span style="color:blue =<span style="color:black "<span style="color:blue 2011-03-21T13:36:00.096-04:00<span style="color:black "<span style="color:blue >

- <span style="color:blue <<span style="color:#a31515 config<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 logoURL<span style="color:blue >http://i2.cdn.turner.com/dr/ncaa/ncaa/release/sites/default/files/images/logos/schools/[teamlogo].17.png<span style="color:blue </<span style="color:#a31515 logoURL<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 deepLinkURL<span style="color:blue >window.open,http://www.ncaa.com/mmod/player/console.html?gameID=[gameID],mmodWindow,width=990,height=721,top=0,left=0,menubar=no,toolbar=no,status=no,location=no<span style="color:blue </<span style="color:#a31515 deepLinkURL<span style="color:blue >

<span style="color:blue </<span style="color:#a31515 config<span style="color:blue >

- <span style="color:blue <<span style="color:#a31515 games<span style="color:blue >

- <span style="color:blue <<span style="color:#a31515 game <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 404<span style="color:black " <span style="color:red time<span style="color:blue =<span style="color:black "<span style="color:blue 1301007600<span style="color:black " <span style="color:red network<span style="color:blue =<span style="color:black "<span style="color:blue CBS<span style="color:black " <span style="color:red state<span style="color:blue =<span style="color:black "<span style="color:blue 1<span style="color:black " <span style="color:red period<span style="color:blue =<span style="color:black "<span style="color:blue " clock="<span style="color:black "<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 team <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 626<span style="color:black " <span style="color:red type<span style="color:blue =<span style="color:black "<span style="color:blue home<span style="color:black " <span style="color:red abbrev<span style="color:blue =<span style="color:black "<span style="color:blue SDSU<span style="color:black " <span style="color:red logo<span style="color:blue =<span style="color:black "<span style="color:blue s/san-diego-st<span style="color:black " <span style="color:red seed<span style="color:blue =<span style="color:black "<span style="color:blue 2" score="<span style="color:black " <span style="color:blue />

<span style="color:blue <<span style="color:#a31515 team <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 164<span style="color:black " <span style="color:red type<span style="color:blue =<span style="color:black "<span style="color:blue visitor<span style="color:black " <span style="color:red abbrev<span style="color:blue =<span style="color:black "<span style="color:blue UCONN<span style="color:black " <span style="color:red logo<span style="color:blue =<span style="color:black "<span style="color:blue c/connecticut<span style="color:black " <span style="color:red seed<span style="color:blue =<span style="color:black "<span style="color:blue 3" score="<span style="color:black " <span style="color:blue />

<span style="color:blue </<span style="color:#a31515 game<span style="color:blue >

- <span style="color:blue <<span style="color:#a31515 game <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 408<span style="color:black " <span style="color:red time<span style="color:blue =<span style="color:black "<span style="color:blue 1301008500<span style="color:black " <span style="color:red network<span style="color:blue =<span style="color:black "<span style="color:blue TBS<span style="color:black " <span style="color:red state<span style="color:blue =<span style="color:black "<span style="color:blue 1<span style="color:black " <span style="color:red period<span style="color:blue =<span style="color:black "<span style="color:blue " clock="<span style="color:black "<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 team <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 235<span style="color:black " <span style="color:red type<span style="color:blue =<span style="color:black "<span style="color:blue home<span style="color:black " <span style="color:red abbrev<span style="color:blue =<span style="color:black "<span style="color:blue FLA<span style="color:black " <span style="color:red logo<span style="color:blue =<span style="color:black "<span style="color:blue f/florida<span style="color:black " <span style="color:red seed<span style="color:blue =<span style="color:black "<span style="color:blue 2" score="<span style="color:black " <span style="color:blue />

<span style="color:blue <<span style="color:#a31515 team <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 77<span style="color:black " <span style="color:red type<span style="color:blue =<span style="color:black "<span style="color:blue visitor<span style="color:black " <span style="color:red abbrev<span style="color:blue =<span style="color:black "<span style="color:blue BYU<span style="color:black " <span style="color:red logo<span style="color:blue =<span style="color:black "<span style="color:blue b/byu<span style="color:black " <span style="color:red seed<span style="color:blue =<span style="color:black "<span style="color:blue 3" score="<span style="color:black " <span style="color:blue />

<span style="color:blue </<span style="color:#a31515 game<span style="color:blue >

- <span style="color:blue <<span style="color:#a31515 game <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 403<span style="color:black " <span style="color:red time<span style="color:blue =<span style="color:black "<span style="color:blue 1301016600<span style="color:black " <span style="color:red network<span style="color:blue =<span style="color:black "<span style="color:blue CBS<span style="color:black " <span style="color:red state<span style="color:blue =<span style="color:black "<span style="color:blue 1<span style="color:black " <span style="color:red period<span style="color:blue =<span style="color:black "<span style="color:blue " clock="<span style="color:black "<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 team <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 193<span style="color:black " <span style="color:red type<span style="color:blue =<span style="color:black "<span style="color:blue home<span style="color:black " <span style="color:red abbrev<span style="color:blue =<span style="color:black "<span style="color:blue DUKE<span style="color:black " <span style="color:red logo<span style="color:blue =<span style="color:black "<span style="color:blue d/duke<span style="color:black " <span style="color:red seed<span style="color:blue =<span style="color:black "<span style="color:blue 1" score="<span style="color:black " <span style="color:blue />

<span style="color:blue <<span style="color:#a31515 team <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 29<span style="color:black " <span style="color:red type<span style="color:blue =<span style="color:black "<span style="color:blue visitor<span style="color:black " <span style="color:red abbrev<span style="color:blue =<span style="color:black "<span style="color:blue ARIZ<span style="color:black " <span style="color:red logo<span style="color:blue =<span style="color:black "<span style="color:blue a/arizona<span style="color:black " <span style="color:red seed<span style="color:blue =<span style="color:black "<span style="color:blue 5" score="<span style="color:black " <span style="color:blue />

<span style="color:blue </<span style="color:#a31515 game<span style="color:blue >

- <span style="color:blue <<span style="color:#a31515 game <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 407<span style="color:black " <span style="color:red time<span style="color:blue =<span style="color:black "<span style="color:blue 1301018100<span style="color:black " <span style="color:red network<span style="color:blue =<span style="color:black "<span style="color:blue TBS<span style="color:black " <span style="color:red state<span style="color:blue =<span style="color:black "<span style="color:blue 1<span style="color:black " <span style="color:red period<span style="color:blue =<span style="color:black "<span style="color:blue " clock="<span style="color:black "<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 team <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 796<span style="color:black " <span style="color:red type<span style="color:blue =<span style="color:black "<span style="color:blue home<span style="color:black " <span style="color:red abbrev<span style="color:blue =<span style="color:black "<span style="color:blue WISC<span style="color:black " <span style="color:red logo<span style="color:blue =<span style="color:black "<span style="color:blue w/wisconsin<span style="color:black " <span style="color:red seed<span style="color:blue =<span style="color:black "<span style="color:blue 4" score="<span style="color:black " <span style="color:blue />

<span style="color:blue <<span style="color:#a31515 team <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 87<span style="color:black " <span style="color:red type<span style="color:blue =<span style="color:black "<span style="color:blue visitor<span style="color:black " <span style="color:red abbrev<span style="color:blue =<span style="color:black "<span style="color:blue BUTLER<span style="color:black " <span style="color:red logo<span style="color:blue =<span style="color:black "<span style="color:blue b/butler<span style="color:black " <span style="color:red seed<span style="color:blue =<span style="color:black "<span style="color:blue 8" score="<span style="color:black " <span style="color:blue />

<span style="color:blue </<span style="color:#a31515 game<span style="color:blue >

<span style="color:blue </<span style="color:#a31515 games<span style="color:blue >

- <span style="color:blue <<span style="color:#a31515 video <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue 13249021<span style="color:black "<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 desc<span style="color:blue >Bernard James Jam<span style="color:blue </<span style="color:#a31515 desc<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 url<span style="color:blue >http://vod.turner.ncaa.com/ncaa/big/mmod/2011/03/20/312_13249021_ncaa_384x216_400K.mp4<span style="color:blue </<span style="color:#a31515 url<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 trt<span style="color:blue >00:00:08<span style="color:blue </<span style="color:#a31515 trt<span style="color:blue >

<span style="color:blue </<span style="color:#a31515 video<span style="color:blue >

<span style="color:blue </<span style="color:#a31515 mmodAdWidget<span style="color:blue >



[/code]

<div style="background-color:white; color:black
<pre>Here is what I came up with:

[/code]

<div style="background-color:white; color:black
<pre><span style="color:blue <<span style="color:#a31515 pre <span style="color:red lang<span style="color:blue =<span style="color:black "<span style="color:blue x-c#<span style="color:black "<span style="color:blue > private void GetFeeds()

{



string homeScore = "";

string awayScore = "";

string homeAbbr = "";

string awayAbbr = "";



XDocument RSSXml = new XDocument();

listBox1.Items.Clear();

try

{

RSSXml = XDocument.Load(_feedUrl);

RSSXml.Save("Feeds.xml");

StatusLabel1.Text = "OK";

}

catch (WebException ex)

{

StatusLabel1.Text = ex.Message;

}

var xmlfeed = XDocument.Load("feeds.xml");







var xml = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),

new XElement("LastUsedSettings",

new XElement("TimerInterval",

new XElement("Interval", Convert.ToString(numericUpDown1.Value))),

new XElement("FeedURL", _feedUrl),

new XElement("LastUpdated", DateTime.Now.ToString())));

XElement EventItems = new XElement("EventItems");



used = new List<span style="color:blue <<span style="color:#a31515 String<span style="color:blue >();



var query = xmlfeed.Descendants("game")

.Where(w => w.Attribute("network") != null && isnotused(w.Attribute("network").Value)).

Select(s => new

{

network = s.Attribute("network").Value,



teamValues = s.Elements("team")

.Select(f => new

{

teamType = f.Attribute("type").Value,

abbr = f.Attribute("abbrev").Value,

score = f.Attribute("score").Value

}).ToArray()

}).ToList();



Console.WriteLine("query count: " + query.Count().ToString());



foreach (var c in query)

{

_network = c.network;

if (!string.IsNullOrEmpty(c.network)) _validToPlay = true;

Console.WriteLine("c.score: " + c.network + "VTP: " + _validToPlay.ToString());



for (int i = 0; i < c.teamValues.Count(); i++)

{

if (c.teamValues.teamType == "home")

{

homeScore = c.teamValues.score;

homeAbbr = c.teamValues.abbr;

}

if (c.teamValues.teamType == "visitor")

{

awayScore = c.teamValues.score;

awayAbbr = c.teamValues.abbr;

}

}





switch (_network)

{

case "TruTV":

{

_TruTVhomeTeam = homeAbbr;

_TruTVhomeScore = homeScore;

_TruTVawayTeam = awayAbbr;

_TruTVawayScore = awayScore;

_TruTVValidToPlay = _validToPlay;

}

break;

case "CBS":

{

_CBShomeTeam = homeAbbr;

_CBShomeScore = homeScore;

_CBSawayTeam = awayAbbr;

_CBSawayScore = awayScore;

_CBSValidToPlay = _validToPlay;

}

break;

case "TBS":

{

_TBShomeTeam = homeAbbr;

_TBShomeScore = homeScore;

_TBSawayTeam = awayAbbr;

_TBSawayScore = awayScore;

_TBSValidToPlay = _validToPlay;

}

break;

case "TNT":

{

_TNThomeTeam = homeAbbr;

_TNThomeScore = homeScore;

_TNTawayTeam = awayAbbr;

_TNTawayScore = awayScore;

_TNTValidToPlay = _validToPlay;

}

break;

default:

{

//_validToPlay = false;

}

break;



}







listBox1.Items.Add(_network);

listBox1.Items.Add(homeAbbr);

listBox1.Items.Add(homeScore);

listBox1.Items.Add(awayAbbr);

listBox1.Items.Add(awayScore);

}

EventItems.Add(new XElement("TruTVhomename", _TruTVhomeTeam));

EventItems.Add(new XElement("TruTVscore", _TruTVhomeScore));

EventItems.Add(new XElement("TruTVvisitorname", _TruTVawayTeam));

EventItems.Add(new XElement("TruTVvisitorscore", _TruTVawayScore));

EventItems.Add(new XElement("TruTVvalidtoshow", _TruTVValidToPlay.ToString()));



EventItems.Add(new XElement("CBShomename", _CBShomeTeam));

EventItems.Add(new XElement("CBSscore", _CBShomeScore));

EventItems.Add(new XElement("CBSvisitorname", _CBSawayTeam));

EventItems.Add(new XElement("CBSvisitorscore", _CBSawayScore));

EventItems.Add(new XElement("CBSvalidtoshow", _CBSValidToPlay.ToString()));



EventItems.Add(new XElement("TBShomename", _TBShomeTeam));

EventItems.Add(new XElement("TBSscore", _TBShomeScore));

EventItems.Add(new XElement("TBSvisitorname", _TBSawayTeam));

EventItems.Add(new XElement("TBSvisitorscore", _TBSawayScore));

EventItems.Add(new XElement("TBSvalidtoshow", _TBSValidToPlay.ToString()));



EventItems.Add(new XElement("TNThomename", _TNThomeTeam));

EventItems.Add(new XElement("TNTscore", _TNThomeScore));

EventItems.Add(new XElement("TNTvisitorname", _TNTawayTeam));

EventItems.Add(new XElement("TNTvisitorscore", _TNTawayScore));

EventItems.Add(new XElement("TNTvalidtoshow", _TBSValidToPlay.ToString()));





xml.Root.Add(EventItems);

xml.Save("settings.xml");





}



private Boolean isnotused(String network)

{

if (used.Contains(network))

{

return false;

}

else

{

used.Add(network);

return true;

}

}





[/code]

<br/>

View the full article
 
Back
Top