Monday, July 11, 2011

Read from XML file using c#

following sample uses XmlTextReader to read from XML file

XmlTextReader reader = new XmlTextReader(MapPath("~/XML/States.xml"));
while (reader.Read())
{ switch (reader.NodeType)
{

case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
while (reader.MoveToNextAttribute()) // Read the attributes.
Console.Write(" " + reader.Name + "='" + reader.Value + "'");
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;
}
 
//sample XML file
<?xml version="1.0" encoding="utf-8" ?>

<Items<Item ddlValue ="1" ddlText = "Yes" />
<Item ddlValue ="2" ddlText = "No" />
</Items>

No comments:

Post a Comment

If you’re using Visual Studio to build your .NET MAUI app on a Mac, locating the IPA (iOS App Package) file can be a bit tricky

  If you’re using   Visual Studio   to build your   .NET MAUI   app on a   Mac , locating the   IPA (iOS App Package)   file can be a bit tr...