Reading XML file from C# code.

The following code will also help us in getting messages dynamically from XML file which we can use in our application.

First create Message.XML , an XML File

Record Inserted Successfully

Record Updated Successfully

Record Deleted Successfully

Error occur during Process

Now add a class file MessageCode.cs in App_Code folder of your project.

public class MessageCode
//Function to set message from XML file.
public string GetMessage(string MessageId)
XmlDocument xmlDoc;
XmlNode xmlNode;
String xpathExpr;
xmlDoc = new XmlDocument();
xmlDoc.Load(HttpContext.Current.Server.MapPath(“../ReadXMLThroughASPX/Message.xml”)); //Load XML
xpathExpr = “/AemesssageList/Message[@id=’" + MessageId + "’]/DisplayMessage”; //Node Path
xmlNode = xmlDoc.SelectSingleNode(xpathExpr); //Get node by Message ID
return xmlNode.InnerText; //Get Node Text

Next step would be call the function on click event of a button.

protected void btnSubmit_Click(object sender, EventArgs e)
MessageCode messageCode = new MessageCode();
Response.Write(“Message : ” + messageCode.GetMessage(“3″));

Output : Message : Record Deleted Successfully

Write to me ( ) in case of any queries.