Read XML File and get the values using C#

Last Reply on Aug 24, 2016 11:50 AM By dharmendr

Posted on Aug 23, 2016 10:06 AM

I have this code to read XML file

var fileName = "D:\\info1.xml";

XDocument doc = XDocument.Load(fileName);
var col = from dummy in doc.DescendantNodes() select dummy;

foreach (var myvar in col)
	//Get the code and description here

The XML file has this element? how can I get the value of code and desc?

<maritalStatus code="M" desc="Married">

Posted on Aug 24, 2016 11:50 AM

Hi florenz,

Refer the below code.

DataSet ds = new DataSet();
string code = string.Empty;
string desc = string.Empty;
foreach (DataRow dr in ds.Tables["maritalStatus"].Rows)
    code = dr["code"].ToString();
    desc = dr["desc"].ToString();