I need to get some values ​​from XML
For example, i need to get: News Name, and News Date from:
<info>
<news>
<news name="Test 1" date="08.13.2013"/>
<news name="Test 2" date="08.09.2013"/>
</news>
</info>
using C#, to show this in Console..
I've tried several codes, which I found on the internet. But none served me!
EDIT¹: I've tried the following codes:
XmlDocument Doc = new XmlDocument();
Doc.Load(Server.MapPath("Content/Doc.xml"));
XmlNodeList itemList = Doc.DocumentElement.SelectNodes("news");
foreach (XmlNode currNode in itemList)
{
string name = string.Empty; <!-- here I make a new string and call it
"date"-->
XmlNode item = currNode.SelectSingleNode("news");
name = item.Attributes["name"].Value.ToString();
Console.WriteLine("Test Name: "+name+"");
}
and:
XmlDocument xml = new XmlDocument();
xml.LoadXml(myXmlString);
XmlNodeList xnList = xml.SelectNodes("/news/news");
foreach (XmlNode xn in xnList)
{
string name = xn["name"].InnerText;
string date = xn["date"].InnerText;
Console.WriteLine("Test: {0} {1}", name, date);
}
But it did not work, and nothing appears. or the program closes
automatically.
No comments:
Post a Comment