hi...I had to read a xml file in c#...i Had written the following code
but the desired output is not coming..

using System;
using System.Xml;

namespace ReadXMLfromFile
{
   
   class Class1
   {
      static void Main(string[] args)
      {
         XmlTextReader reader = new XmlTextReader("my.xml");
         while (reader.Read())
         {
            switch (reader.NodeType)
            {
               case XmlNodeType.Element: // The node is an element.
                  Console.Write("<" + reader.Name);
                  Console.WriteLine(">");
                  break;
               case XmlNodeType.Text: //Display the text in each element.
                  Console.WriteLine(reader.Value);
                  break;
               case XmlNodeType.EndElement: //Display the end of the element.
                  Console.Write("</" + reader.Name);
                  Console.WriteLine(">");
                  break;
            }
         }
         Console.ReadLine();
      }
   }
}

the xml file i need to read is

<?xml version="1.0" encoding="utf-8" ?>
<Workflow>
  <User UserName="abc" Sirname="xyz"/>
  
</Workflow>

the output i am getting is:
<Workflow>
<User>
</Workflow>

Recommended Answers

All 2 Replies

I think you forgot to read attributes.

if (reader.HasAttributes)
            {
                Console.WriteLine(reader.GetAttribute("Username"));
            }

I think you forgot to read attributes.

if (reader.HasAttributes)
            {
                Console.WriteLine(reader.GetAttribute("Username"));
            }

thanks, par iam not able to read this,
rather this code is working:

strn=reader.GetAttribute(0);
                     strsn=reader.GetAttribute(1);
                     Console.WriteLine(strn);
                     Console.WriteLine(strsn);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.