0

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>

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by techstu
0

I think you forgot to read attributes.

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

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);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.