Hello, i have this code which reades the nodes and then populates a combolist. Could someone help me do the same with attributes ? :)
// Code
private void populateCombo()
{
cmbPlatform.Items.Clear();
XmlDocument doc = new XmlDocument();
doc.Load("C:/Users/Daxal/Desktop/example/VideoGames.xml");
XmlNodeList nodeList = doc.SelectNodes("TGames/Game");
foreach(XmlNode node in nodeList)
if (!cmbPlatform.Items.Contains(node.SelectSingleNode("Name").InnerText))
cmbPlatform.Items.Add(node.SelectSingleNode("Name").InnerText);
}
Suthish NairPosted Dec 19, 2010, 5:45 AM
replace your code with...
Daniel GolanPosted Dec 19, 2010, 6:32 AM
Suthish NairPosted Dec 19, 2010, 6:28 AM
Daniel GolanPosted Dec 19, 2010, 6:21 AM
ex. response = console
Daniel GolanPosted Dec 19, 2010, 4:47 AM
In the c# i refered to a local version iof teh same xml, and copied a part from therejus to show the structure.
Suthish NairPosted Dec 19, 2010, 4:38 AM
It seems xml contains some invalid characters, can you attach/upload the correct xml. Do not copy paste.
Daniel GolanPosted Dec 18, 2010, 5:59 PM
the only thing i cant get to work is reading a specific set of nodes which seems wierd but ive been trying all day.. Think i got all the code right, but cant get it to work ..
Anyone have an idea ?
XDocument xmlDoc = XDocument.Load("C:/Users/Daxal/documents/visual studio 2010/Projects/Yr/Yr/fjellstrand.xml");
var games = from game in xmlDoc.Descendants("weatherdata")
where game.Element("forecast").Element("text").Element("location").Element("time").Attribute("from").Value == cmb_sted.SelectedItem.ToString()
select new
{
Title = game.Element("title").Value,
Body = game.Element("body").Value,
};
txt_result.Text = "";
foreach (var game in games)
{txt_result.Text = txt_result.Text + "Title : " + game.Title + "\n";
txt_result.Text = txt_result.Text + "Body : " + game.Body + "\n\n";
}
if (txt_result.Text == "")
txt_result.Text = "No Results.";
The xml
<weatherdata>
<location>
<name>Fjellstrandname>
<type>Tettstedtype>
<country>Norgecountry>
<timezone id="Europe/Oslo" utcoffsetMinutes="60" />
<location altitude="38" latitude="59.794154359539" longitude="10.6068855642781" geobase="ssr" geobaseid="73350" />
location>
<credit>
url="http://www.yr.no/sted/Norge/Akershus/Nesodden/Fjellstrand/" />
credit>
<links>
<link id="radar" url="http://www.yr.no/sted/Norge/Akershus/Nesodden/Fjellstrand/radar.html" />
links>
<meta>
<lastupdate>2010-12-18T10:30:00lastupdate>
<nextupdate>2010-12-18T17:15:00nextupdate>
meta>
<sun rise="2010-12-18T09:15:25" set="2010-12-18T15:12:43" />
<forecast>
<text>
<location name="Fjellstrand">
<time from="2010-12-17" to="2010-12-18">
The part under is what i have to read.
<title>fredag og lørdagtitle>
<body><strong>Østlandet:</strong> Nordlig bris, liten kuling utsatte steder, lørdag dreiende østlig, på kysten nordøstlig. Snø, fra lørdag formiddag lettere vær, stort sett opphold øst for Oslo og i nordlige områder.body>
time>
Suthish NairPosted Dec 18, 2010, 12:56 AM
Remember to mark the answer as accepted.
Daniel GolanPosted Dec 17, 2010, 9:20 PM
Working abit on the code you helped with made it work almost perfect, the only thing im really missing is that when the items get added to the combolist theyr name is id="1", could you please help me only get the value (1) ?
Code
private void populateCombo()
{
cmbPlatform.Items.Clear();
XDocument xmlDoc = XDocument.Load("C:/Users/Daxal/Desktop/example/VideoGames.xml");
var xmlAtt = from att in xmlDoc.Elements("VGames").Elements("Game").Attributes("id")
select att;
foreach (var record in xmlAtt)
{
if (!cmbPlatform.Items.Contains(record));
cmbPlatform.Items.Add(record);
}
}
XML :
xml version="1.0" encoding="utf-8" ?>
<VGames>
<Game>
<Title id="1">Grand Theft Auto IVTitle>
<Platform>XBOX360Platform>
Game>
<Game>
Suthish NairPosted Dec 17, 2010, 1:46 PM
Daniel GolanPosted Dec 17, 2010, 1:29 PM
Suthish NairPosted Dec 16, 2010, 1:20 PM