XNamespace xNamespace = "http://www.w3.org/TR/html4/"; //replace this with namespace of prefix 'yweather'
XDocument xDocument = XDocument.Load(@"Ur xml path");
var result = from item in xDocument.Descendants(xNamespace + "forecast")
select item;
foreach (XElement item in result)
{
var day = item.Attribute("day").Value;
}
Ramesh is right,if you want to fetch some nodes with a special node——with namespace,you have to define its namespace so as to let the node itself know what it is……
It seems that you are now using LINQ-TO-XML,in fact you can also use a traditional way with the help of XmlDocument put in the namespace of System.Xml,Here's some snippet of codes to let you know more about how to use this:
AmalO.Abdull...
Contributor
3116 Points
764 Posts
XElement
Jun 04, 2012 01:08 PM|LINK
Hi all
i have an Xelement as follow
<yweather:forecast day="Mon" date="4 Jun 2012" text="test test" code="30" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" />
and i tried to get the attributes which is day , date but it's giving me nothing ,, who can tell me how
DayName = forecastItem.Attribute("day").Value
Ramesh T
Contributor
5121 Points
827 Posts
Re: XElement
Jun 04, 2012 03:07 PM|LINK
Try this
XNamespace xNamespace = "http://www.w3.org/TR/html4/"; //replace this with namespace of prefix 'yweather' XDocument xDocument = XDocument.Load(@"Ur xml path"); var result = from item in xDocument.Descendants(xNamespace + "forecast") select item; foreach (XElement item in result) { var day = item.Attribute("day").Value; }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: XElement
Jun 06, 2012 01:35 AM|LINK
Ramesh is right,if you want to fetch some nodes with a special node——with namespace,you have to define its namespace so as to let the node itself know what it is……
It seems that you are now using LINQ-TO-XML,in fact you can also use a traditional way with the help of XmlDocument put in the namespace of System.Xml,Here's some snippet of codes to let you know more about how to use this:
http://msdn.microsoft.com/en-us/library/h0hw012b.aspx