如何获得子节点&使用C#动态生成父节点的子节点的值 [英] How to get child node & value of child node of a parent node dynamically using C#

查看:87
本文介绍了如何获得子节点&使用C#动态生成父节点的子节点的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

我正在尝试使用c#动态创建xml文件.我想创建xml文件,如下所示:

Hi friends,

i m trying to creat a xml file dynamically using c#.i want to create xml file like this :

<?xml version="1.0" encoding="utf-8" ?>
- <Hospitalizations>
- <Hospitalization Id="">
  <Patient_Id>REG-10010</Patient_Id>
  <FileId>REG-10010</FileId>
  <date_of_admission>29/07/2010 13:56:16</date_of_admission>
  <date_of_discharge>31/07/2010 14:55:49</date_of_discharge>
<ward Date="08/12/2011">General</ward>
  <admission_type>Genral</admission_type>
- <Wards>
  <ward Date="08/12/2011">General</ward>
  <ward Date="08/12/2011">General2</ward>
  </Wards>
  <primary_department>Obs. & Gynae</primary_department>
  <primary_doctor_name>Vandana Bansal MS,FCGP,D.Phil</primary_doctor_name>
  <discharge_type />
  <discharge_details>31/07/2010 14:55:49,</discharge_details>
- <diagnosis>
  <entry>None</entry>
  </diagnosis>
  </Hospitalization>
  </Hospitalizations>




亲切地建议我如何检索子节点&动态添加到主XML节点.

谢谢&问候
Gulam Hussain




kindly suggest me that how to retrieve child node & Add Into Main XML Node dynamically.

Thanks & Regards
Gulam Hussain

推荐答案

如果您不熟悉Linq< => XML(System.Xml.Linq):[
如果要向节点添加新的子节点:首先使用.Element获取节点:然后在该节点上使用.Add方法:
If you are not familiar with Linq<=>XML (System.Xml.Linq):[^].

If you are already using Linq<=>XML, you might review the use of XElement:[^].

In any case you want your tree-structured information loaded into an XDocument. You can access all the top level nodes of your XDocument using XDocument.Descendants.

You find an Element by using .Element("elementName") method on the parent Node of the Element you are looking for.

If you want to add a new child node to a node: first use .Element to get the node: then use the .Add method on that node:
Hospitalizations.Descendants.Element("Hospitalization1").Add(new XElement("List of Procedures"));

如Addy Tas所建议,XPath(System.Linq.XPath)可能在此处可用于访问元素:[ ^ ].

As suggested by Addy Tas, XPath (System.Linq.XPath) may be useful here for accessing elements:[^].


这篇关于如何获得子节点&amp;使用C#动态生成父节点的子节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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