Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all,

I m trying to show my xml file like this using dataset ds in c#.i retrieve rest of the node value except ward & diagnosis Node value.
-------------------------------------------------------------
HTML
<Hospitalizations>
  <Hospitalization Id="">
    <Patient_Id>REG-10010</Patient_Id>
    <admission_type>Genral</admission_type>
    <Wards>
         <ward Date="08/12/2011">Ward A</ward>
         <ward Date="09/12/2011">Ward B</ward>
    </Wards>   
    <discharge_details>31/07/2010 14:55:49,</discharge_details>
    <diagnosis>
         <entry>Text Entery 1</entry>
         <entry>Text Entery 2</entry>
    </diagnosis>
  </Hospitalization>
</Hospitalizations>
--------------------------------------------------------------
any body have any idea about it.
Thanks & Regards
Huassain
Posted
Updated 21-Dec-11 18:16pm
v2
Comments
Karthik Harve 22-Dec-11 0:16am    
[Edit] pre tags added.

My understanding is that you can access Level 1 (Hospitalization) and Level 2 (Patient_Id,etc), itz hard to pull Level 3 (Wards, Diagnosis)

If that is the case, you can use generic recursive multi level XML node fetch logarithm as:

VB
Function GetChildren(parentNode as TreeNode) as List(Of String)
  Dim nodes as List(Of String) = New List(Of String)
  GetAllChildren(parentNode, nodes)
  return nodes
End Function

Sub GetAllChildren(parentNode as TreeNode, nodes as List(Of String))
  For Each childNode as TreeNode in parentNode.Nodes
    nodes.Add(childNode.Text)
    GetAllChildren(childNode, nodes)
  Next
End Function
 
Share this answer
 
DataSet ds = new DataSet();
ds = Obj_Common.FillDataset("proc_hospitalizationdetails_FORXML 1,'10004',1", "TableName");
if (ds.Tables[0].Rows.Count > 0)
{

/*-------------------------------------------------------------------------------------------*/

XmlDocument xmldoc_PDetail = new XmlDocument();
// Write down the XML declaration
XmlDeclaration xmlDeclaration = xmldoc_PDetail.CreateXmlDeclaration("1.0", null , null);

// Create the root element
XmlElement MainNode = xmldoc_PDetail.CreateElement("Hospitalizations");
xmldoc_PDetail.InsertBefore(xmlDeclaration, xmldoc_PDetail.DocumentElement);
xmldoc_PDetail.AppendChild(MainNode);

// Create a new <Category> element and add it to the root node
XmlElement parentNode = xmldoc_PDetail.CreateElement("Hospitalization");
parentNode.SetAttribute("Id", "");

//for Child Node Of Parent Node
xmldoc_PDetail.DocumentElement.PrependChild(parentNode);

/*************Creat Field For XML File*****************/
// Create the required nodes for Parent Node

XmlElement Patient_ID = xmldoc_PDetail.CreateElement("Patient_Id");//'<![CDATA[<VideoCollection>Personal Collection of Movies</VideoCollection>]]>'
XmlElement File_Id = xmldoc_PDetail.CreateElement("FileId");
XmlElement date_of_admission = xmldoc_PDetail.CreateElement("date_of_admission");
XmlElement date_of_discharge = xmldoc_PDetail.CreateElement("date_of_discharge");
XmlElement admission_type = xmldoc_PDetail.CreateElement("admission_type");
XmlElement ward = xmldoc_PDetail.CreateElement("Wards");
xmldoc_PDetail.DocumentElement.PrependChild(ward);
DataSet ds1 = new DataSet();
try
{

ds1 = Obj_Common.FillDataset("proc_hospitalizationdetails_FORXML 2,'10004',1", "Tableward");
if (ds1.Tables["Tableward"].Rows.Count > 0)
{
int iJ = 1;
foreach (DataRow dr1 in ds1.Tables["Tableward"].Rows)
{
XmlElement WardNode = xmldoc_PDetail.CreateElement("ward"); //Node For Wards Parent Node
XmlText WardNode1 = xmldoc_PDetail.CreateTextNode(dr1["WardName"].ToString().Trim());
//WardNode.SetAttribute("DateTime", DateTime.Now.ToString());
WardNode.AppendChild(WardNode1);
ward.AppendChild(WardNode);
}
//for (int i = 0; i < iJ; i++)
//{

// if (iJ < 4)
// {
// XmlElement WardNode = xmldoc_PDetail.CreateElement("ward"); //Node For Wards Parent Node
// XmlText WardNode1 = xmldoc_PDetail.CreateTextNode("Ward - " + iJ);
// WardNode.SetAttribute("DateTime", DateTime.Now.ToString());
// WardNode.AppendChild(WardNode1);
// ward.AppendChild(WardNode);
// iJ++;

// }
// else
// {
// break;
// }
//}

}
}
catch
{
}
finally
{
ds1.Dispose();
}


XmlElement primary_department = xmldoc_PDetail.CreateElement("primary_department");
XmlElement primary_doctor_name = xmldoc_PDetail.CreateElement("primary_doctor_name");
XmlElement discharge_type = xmldoc_PDetail.CreateElement("discharge_type");
XmlElement discharge_details = xmldoc_PDetail.CreateElement("discharge_details");
XmlElement diagnosis = xmldoc_PDetail.CreateElement("diagnosis");
xmldoc_PDetail.DocumentElement.PrependChild(diagnosis);
DataSet ds2 = new DataSet();
try
{

ds2 = Obj_Common.FillDataset("proc_hospitalizationdetails_FORXML 3,'IP-284',1", "Daignosis");
if (ds1.Tables["Daignosis"].Rows.Count > 0)
{
int iJ = 1;
foreach (DataRow dr2 in ds2.Tables["Daignosis"].Rows)
{
XmlElement entry = xmldoc_PDetail.CreateElement("Entry"); // Node For Wards Parent Node
XmlText entry1 = xmldoc_PDetail.CreateTextNode(dr2["diseasename"].ToString().Trim());
//entry.SetAttribute("DateTime", DateTime.Now.ToString());
entry.AppendChild(entry1);
diagnosis.AppendChild(entry);
}

}
}
catch
{
}
finally
{
ds2.Dispose();
}


/*************Set Field For Value*****************/
//Retrieve the text value for Node 1
XmlText Patient_ID1 = xmldoc_PDetail.CreateTextNode(ds.Tables[0].Rows[0]["PatientID"].ToString().Trim());
XmlText File_Id1 = xmldoc_PDetail.CreateTextNode(ds.Tables[0].Rows[0]["FileID"].ToString().Trim());
XmlText date_of_admission1 = xmldoc_PDetail.CreateTextNode(ds.Tables[0].Rows[0]["AdmissionDate"].ToString().Trim());
XmlText date_of_discharge1 = xmldoc_PDetail.CreateTextNode(ds.Tables[0].Rows[0]["DischargeDate"].ToString().Trim());
XmlText admission_type1 = xmldoc_PDetail.CreateTextNode(ds.Tables[0].Rows[0]["AdmissionType"].ToString().Trim());


XmlText primary_department1 = xmldoc_PDetail.CreateTextNode(ds.Tables[0].Rows[0]["Primary_Department"].ToString().Trim());
XmlText primary_doctor_name1 = xmldoc_PDetail.CreateTextNode(ds.Tables[0].Rows[0]["DoctorName"].ToString().Trim());
XmlText discharge_type1 = xmldoc_PDetail.CreateTextNode(ds.Tables[0].Rows[0]["DischargeType"].ToString().Trim());
XmlText discharge_details1 = xmldoc_PDetail.CreateTextNode(ds.Tables[0].Rows[0]["Discharge_Detail"].ToString().Trim());


/**************************************/

// append the nodes to the parentNode without the value For Node 1
parentNode.AppendChild(Patient_ID);
parentNode.AppendChild(File_Id);
parentNode.AppendChild(date_of_admission);
parentNode.AppendChild(date_of_discharge);
parentNode.AppendChild(admission_type);
parentNode.AppendChild(ward);

parentNode.AppendChild(primary_department);
parentNode.AppendChild(primary_doctor_name);
parentNode.AppendChild(discharge_type);
parentNode.AppendChild(discharge_details);
parentNode.AppendChild(diagnosis);


// save the value of the fields into the nodes for Node 1

Patient_ID.AppendChild(Patient_ID1);
File_Id.AppendChild(File_Id1);
date_of_admission.AppendChild(date_of_admission1);
date_of_discharge.AppendChild(date_of_discharge1);
admission_type.AppendChild(admission_type1);

primary_department.AppendChild(primary_department1);
primary_doctor_name.AppendChild(primary_doctor_name1);
discharge_type.AppendChild(discharge_type1);
discharge_details.AppendChild(discharge_details1);

// Save to the XML file
xmldoc_PDetail.Save(Server.MapPath("pDETAILS.xml"));
Response.Write("XML file created");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900