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

How to upload app to macOS

1. Open Terminal Press Cmd (⌘) + Space , type Terminal , and hit Enter . 2. Navigate to Your Build Output Directory Your .app file is likel...