In a C # visual project, I want to pass an XML document object to a method. In this method, I have to read the values ββstored inside the object of the XML document. Without creating an XML file.
Thanks for the answer guys, I finally got my piece of code.
//use following code when assign values to XMlDocument
XMLOBJECT()
{
XmlDocument xmlEmployee = new XmlDocument();
XmlElement xmlRoot = xmlEmployee.CreateElement("HR");
XmlElement xmlEmployees = xmlEmployee.CreateElement("Employee");
xmlEmployees.SetAttribute("Name", "XYZ");
xmlEmployees.SetAttribute("DOB", "12/12/2010");
xmlRoot.AppendChild(xmlEmployees);
xmlEmployee.AppendChild(xmlRoot);
Employee Emp=new EMployee();
Emp.retriveXMl(xmlEmployee);
}
In the above code, our XML object is now created, we can pass the Xml object.
class employee
{
retrivelXMl(XMLDOCUMENT xmlEmployeeobject)
{
string NAME;
int DOB;
XmlNodeList xmlEmployees = xmlEmployeeobject.SelectNodes("//Employee");
foreach (XmlElement Employee in xmlEmployees)
{
NAME = Employee.GetAttribute("Name"));
DOB = int.parse(Employee.GetAttribute("DOB"));
}
}
}
source
share