Read XML File and get the values using C#

Last Reply one year ago By dharmendr

Posted one year ago

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 one year ago

Hi florenz,

Refer the below code.

DataSet ds = new DataSet();
ds.ReadXml("D:\\info1.xml");
string code = string.Empty;
string desc = string.Empty;
foreach (DataRow dr in ds.Tables["maritalStatus"].Rows)
{
    code = dr["code"].ToString();
    desc = dr["desc"].ToString();
}

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html