1

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.

SOLVED! Thanks, for all!

I use this code:

static void Main(string[] args)
        {
            XElement rootElement = XElement.Load(@"http://mywebsite.com/test.xml");

            Program test = new Program();
            Console.WriteLine(test.GetOutline(0, rootElement));  
        }

        private string GetOutline(int indentLevel, XElement element)
        {
            StringBuilder result = new StringBuilder();

            if (element.Attribute("name") != null)
            {
                result = result.AppendLine(new string(' ', indentLevel * 2) + element.Attribute("name").Value);
            }

            foreach (XElement childElement in element.Elements())
            {
                result.Append(GetOutline(indentLevel + 1, childElement));
            }

            return result.ToString();
        }

Result: Test 1 Test 2

Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

1

I like to use Linq to XML for this kind of thing.

I tested this snippet in LinqPad... Should give you an idea of how to do this.

var xml = @"<info>
                <news>
                    <news name='Test 1' date='08.13.2013'/>
                    <news name='Test 2' date='08.09.2013'/>
                </news>
            </info>";

var doc = XElement.Parse(xml);

var list = from x in doc.DescendantsAndSelf("news")
            where !string.IsNullOrEmpty((string)x.Attribute("name"))
            select new {name = (string)x.Attribute("name"), date = (DateTime)x.Attribute("date")};
list.Dump();
Tim Jarvis
  • 18,465
  • 9
  • 55
  • 92
0

It's complicated a slight amount by having your news container holding news elements. But the basic xml manipulation looks like...

XDocument xdoc = XDocument.Load(Server.MapPath("Content/Doc.xml"));

var newsParent = xdoc.Descendants("news").First();  // gets the parent news item
var news = newsParent.Descendants("news")
    .Select(x => new {Name = x.Attribute("name"), Date = x.Attribute("date")});
foreach (var element in news)
{
    Console.WriteLine(element.Name +" "+ element.Date);
}
Kevin
  • 4,586
  • 23
  • 35
0

Linq to Xml may be a better option as feching data is a lot simpler and direct,

For example one way to get your data based on the xml sample you provided could be something like this:

XElement data = XElement.Load(@"C:\Test.xml");

var newsResults = data.Element("news")
               .Descendants("news")
               .Select(x => new
               {
                   Name = (string)x.Attribute("name"),
                   Date = (DateTime)x.Attribute("date")
               });

foreach (var news in newsResults)
{
    Console.WriteLine("News: {0}, Date: {1}", news.Name, news.Date);
}
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110