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

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

XML
<?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>



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

Thanks & Regards
Gulam Hussain
Posted
Updated 15-Dec-19 23:20pm
v4
Comments
RaviRanjanKr 9-Dec-11 4:49am    
[Edited]Code is wrapped in Pre tag[/Edited]
RAJI @Codeproject 9-Dec-11 5:31am    
lot of code!!!!!!!
Addy Tas 10-Dec-11 10:02am    
Have you looked at XPath and XmlDocument?
Cheers, AT

1 solution

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"));
As suggested by Addy Tas, XPath (System.Linq.XPath) may be useful here for accessing elements:[^].
 
Share this answer
 
v2
Comments
Wonde Tadesse 10-Dec-11 17:47pm    
5+

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