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 NairRank #92 · 22.8K reputationPosted Dec 19, 2010, 12:45 AM EST
replace your code with...
using (XmlTextReader reader = new XmlTextReader(Server.MapPath("1.xml"))) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "title" || reader.Name == "body") { Response.Write(Server.HtmlDecode(reader.ReadInnerXml()) + " "); } } } }Suthish NairRank #92 · 22.8K reputationPosted Dec 16, 2010, 8:20 AM EST
using System.Collections.Generic; using System.Xml.Linq; using System.Linq;private void populateCombo() { cmbPlatform.Items.Clear(); XDocument xmlDoc = XDocument.Load("C:/Users/Daxal/Desktop/example/VideoGames.xml"); var xmlAtt = from att in xmlDoc.Elements("TGames").Elements("Game") select att; foreach (var record in xmlAtt) { put breakpoint in record and check the value, according to that baind the records //cmbPlatform.Items.Add } }Daniel Golan45 reputationPosted Dec 17, 2010, 8:29 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Dec 17, 2010, 8:46 AM EST
Daniel Golan45 reputationPosted Dec 17, 2010, 4:20 PM EST
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 NairRank #92 · 22.8K reputationPosted Dec 17, 2010, 7:56 PM EST
Remember to mark the answer as accepted.
XDocument xmlDoc = XDocument.Load(Server.MapPath("1.xml")); var xmlAtt = from att in xmlDoc.Elements("VGames").Elements("Game").Elements("Title").Attributes("id") where att.Value == "1" select att; foreach (var record in xmlAtt) { Response.Write(record.Parent.Value + " "); }Daniel Golan45 reputationPosted Dec 18, 2010, 12:59 PM EST
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 NairRank #92 · 22.8K reputationPosted Dec 18, 2010, 11:38 PM EST
It seems xml contains some invalid characters, can you attach/upload the correct xml. Do not copy paste.
Daniel Golan45 reputationPosted Dec 18, 2010, 11:47 PM EST
In the c# i refered to a local version iof teh same xml, and copied a part from therejus to show the structure.
Daniel Golan45 reputationPosted Dec 19, 2010, 1:21 AM EST
ex. response = console
Suthish NairRank #92 · 22.8K reputationPosted Dec 19, 2010, 1:28 AM EST
Daniel Golan45 reputationPosted Dec 19, 2010, 1:32 AM EST