how to read XML file in C#?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml;

namespace XML_READ_FILE

{

class Program

{

static void Main(string[] args)

{

XmlTextReader rdfile = new XmlTextReader("Documents://food.xml");

while (rdfile.Read())

{

switch (rdfile.NodeType)

{

case XmlNodeType.Element:

Console.WriteLine("<" + rdfile.Name);

while (rdfile.MoveToNextAttribute())

{

Console.Write(" " + rdfile.Name + "= '"

+rdfile.Value + " '");

}

break;

case XmlNodeType.Text:

Console.WriteLine(rdfile.Value);

break;

case XmlNodeType.EndElement:

Console.WriteLine(("</" + rdfile.Name + ">"));

break;

}

}

}

}

}

im new in XML

why rdfile.read() does not work?

tanx

3 Answers

Relevance
  • 9 years ago
    Favorite Answer

    Did you try stepping through the code with the debugger and see what happens or what is actually being read by this routine?

    Source(s): VB.NET Programmer
    • Login to reply the answers
  • 3 years ago

    the only unbelievable respond appeared adequate, although this might develop into fairly not challenge-unfastened in case you desire the employing to allocate the variables in the enter checklist dynamically. at modern, I have not have been given any way of doing this. Sorry i'll't furnish help to from now on than that.

    • Login to reply the answers
  • 9 years ago

    it may be finding the file. either debug and step through the program or put an if statement in to check rdfile != null and go from there.

    also, I think XmlDocument.Save(Console.Out) would do what your doing above.. not entirely sure, but there you go

    • Login to reply the answers
Still have questions? Get your answers by asking now.