如何使用C#在xml中创建节点的智利节点 [英] how to create chile node of a node in xml using c#

查看:56
本文介绍了如何使用C#在xml中创建节点的智利节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试使用c#中的数据集ds显示这样的xml文件,我检索了除ward&以外的其余节点值.诊断节点值.
-------------------------------------------------- -----------

 <  医院住院 > ; 
  <  医院住院    ="  " > 
    <  患者ID  >  REG-10010 <  /Patient_Id  > 
    <   admission_type  > 一般<  /admission_type  > 
    <  围场 > 
         <  病房    ="   2011/08/12" > 区A <  /ward  > 
         <  病房    ="   2011/12/12" >  Ward B <  /ward  > 
    <  /沃兹 > 
    <   discharge_details  >  31/07 /2010 14:55:49,<  /discharge_details   > 
    <  诊断 > 
         <  条目 > 文本Entery 1 <  /entry  > 
         <  条目 > 文本Entery 2 <  /entry  > 
    <  /diagnosis  > 
  <  /医院住院 > 
<  /医院住院 >   ------------------------------------------------ -------------- 
任何人对此都有任何想法.
谢谢&问候
Huassain

解决方案

我的理解是,您可以访问1级(住院)和2级(患者ID等),很难达到3级(病房,诊断)

在这种情况下,可以将通用递归多级XML节点获取对数用作:

 功能 GetChildren(parentNode  as  TreeNode) Of   Dim 节点 as 列表( Of  字符串)= 新建列表( Of  字符串)
  GetAllChildren(parentNode,节点)
  返回节点
结束 功能

 Sub  GetAllChildren(parentNode  as  TreeNode,节点 as 列表( Of   String ))
  对于 每个 childNode  as  TreeNode  parentNode.Nodes中
    nodes.Add(childNode.Text)
    GetAllChildren(childNode,节点)
  下一步
结束 功能 


DataSet ds = new DataSet ();
ds = Obj_Common.FillDataset("proc_hospitalizationdetails_FORXML 1," 10004,1","TableName");
如果(ds.Tables [0] .Rows.Count> 0)
{

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

XmlDocument xmldoc_PDetail =新的XmlDocument();
//写下XML声明
XmlDeclaration xmlDeclaration = xmldoc_PDetail.CreateXmlDeclaration("1.0",null,null);

//创建根元素
XmlElement MainNode = xmldoc_PDetail.CreateElement(医院化");
xmldoc_PDetail.InsertBefore(xmlDeclaration,xmldoc_PDetail.DocumentElement);
xmldoc_PDetail.AppendChild(MainNode);

//创建一个新的< Category>元素并将其添加到根节点
XmlElement parentNode = xmldoc_PDetail.CreateElement(医院住院");
parentNode.SetAttribute("Id",");

//对于父节点的子节点
xmldoc_PDetail.DocumentElement.PrependChild(parentNode);

/*************为XML文件创建字段*****************/
//为父节点创建所需的节点

XmlElement Patient_ID = xmldoc_PDetail.CreateElement("Patient_Id");//''<![CDATA [< VideoCollection>电影的个人收藏</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");
XmlElementmission_type = xmldoc_PDetail.CreateElement("admission_type");
XmlElement病房= xmldoc_PDetail.CreateElement("Wards");
xmldoc_PDetail.DocumentElement.PrependChild(ward);
DataSet ds1 =新的DataSet();
试试
{

ds1 = Obj_Common.FillDataset("proc_hospitalizationdetails_FORXML 2," 10004,1","Tableward");
如果(ds1.Tables ["Tableward"].Rows.Count> 0)
{
int iJ = 1;
foreach(ds1.Tables ["Tableward"].rows中的DataRow dr1)
{
XmlElement WardNode = xmldoc_PDetail.CreateElement("ward"); //病房节点父节点
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 ++)
//{

//如果(iJ< 4)
//{
//XmlElement WardNode = xmldoc_PDetail.CreateElement("ward"); //病房节点父节点
//XmlText WardNode1 = xmldoc_PDetail.CreateTextNode("Ward-" + iJ);
//WardNode.SetAttribute("DateTime",DateTime.Now.ToString());
//WardNode.AppendChild(WardNode1);
//ward.AppendChild(WardNode);
//iJ ++;

//}
//else
//{
//中断;
//}
//}

}
}
赶上
{
}
终于
{
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诊断= xmldoc_PDetail.CreateElement("diagnosis");
xmldoc_PDetail.DocumentElement.PrependChild(diagnosis);
DataSet ds2 =新的DataSet();
试试
{

ds2 = Obj_Common.FillDataset("proc_hospitalizationdetails_FORXML 3," IP-284,1","Daignosis");
if(ds1.Tables ["Daignosis"].Rows.Count> 0)
{
int iJ = 1;
foreach(ds2.Tables ["Daignosis"].行中的DataRow dr2)
{
XmlElement entry = xmldoc_PDetail.CreateElement("Entry"); //病房节点父节点
XmlText entry1 = xmldoc_PDetail.CreateTextNode(dr2 [疾病名称"] .ToString().Trim());
//entry.SetAttribute("DateTime,DateTime.Now.ToString());
entry.AppendChild(entry1);
diagnostic.AppendChild(entry);
}

}
}
赶上
{
}
终于
{
ds2.Dispose();
}


/*************设置值字段*****************/
//检索节点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());
XmlTextmission_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());


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

//将节点附加到不带值对于节点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);


//将字段的值保存到节点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);
mission_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);

//保存到XML文件
xmldoc_PDetail.Save(Server.MapPath("pDETAILS.xml"));
Response.Write(创建XML文件");
}


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.
-------------------------------------------------------------

<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

解决方案

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:

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


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");
}


这篇关于如何使用C#在xml中创建节点的智利节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆