1

Friends, My school project is having an xml data file:

<patients>
<patient>
<regNo>2012/Mar/003</regNo>
<name>Jhon</name>
<add>Somewhere</add>
<mobile>0000</mobile>
.
.
.
<stay>2</stay>
<costofroom>100</costofroom>
<total>200</total>
</patient>
</patients>

My Windowsform "EditPatients_Load" is able to fetch all info of patient Jhon, and now let's assume that the Admin needs to change some information in the form & resubmit.

Then how to write back all values to Jhon's account in the same xml file????

I'm not able to makeup the logical code, even if I check the node if (patients.paptient.name = "nameComboBox.text").... how to make sure that I'm writing other values on proper place?

Rgrdz,

gsvirdi
  • 406
  • 2
  • 7
  • 22
  • What do you mean by `how to make sure that I'm writing other values on proper place` ?? Can you maybe rephrase it? – gideon Apr 03 '12 at 04:38
  • What do you mean writing to the write place you either update file by replacing values add new data or rewrite files in this case it will be the same file with more data – COLD TOLD Apr 03 '12 at 04:40
  • sorry for late reply: what I mean was how can I be sure that submission of all winform values are going to be updated into a particular child node (Jhon's account). Bcoz to keep the xml layout simple I made a simple:Root/parent then child1, child2, child3.... nodes layout – gsvirdi Apr 03 '12 at 05:56

3 Answers3

2

Try this:

//string xml = 
            //@"<patients><patient><regNo>2012/Mar/003</regNo><name>Jhon</name><add>Somewhere
            //</add><mobile>0000</mobile><stay>2</stay><costofroom>100</costofroom><total>200</total>
            //</patient></patients>";
            XDocument xmlDoc = XDocument.Load(@"c:\abc.xml");
            var items = (from item in xmlDoc.Descendants("patient")
                         where item.Element("name").Value == "Jhon"
                         select item);
            if (items.Count() > 0)
            {
                var item = items.First();
                item.SetElementValue("add", "New New Address");
                xmlDoc.Save(@"c:\abc.xml", SaveOptions.None);
            }

You can get single element using

var item = (from item in xmlDoc.Descendants("patient")
                             where item.Element("name").Value == "Jhon"
                             select item).FirstOrDefault();

then update it using SetElementValue() method.

//Updated Xml

<?xml version="1.0" encoding="utf-8"?>
<patients>
  <patient>
    <regNo>2012/Mar/003</regNo>
    <name>Jhon</name>
    <add>New Address</add>
    <mobile>0000</mobile>
    <stay>2</stay>
    <costofroom>100</costofroom>
    <total>200</total>
  </patient>
</patients>

Reference:
Update XML with C# using Linq

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • Dear Niranjan, I'm a bit confused about the working of the code. I'm confused that..... I don't know which field a user may edit, so I've to re-write all fields into the xml. But, When I'll resubmit the form, will all other nodes like Registration No, Add, Mobile, etc related to patient Jhon will get updated using the same example code or not? in the meanwhile I'm trying.... – gsvirdi Apr 03 '12 at 06:39
  • you can update all for particular user, just use the foreach loop on items. and update properties one by one. `item.SetElementValue("add", txtAddress.Text); item.SetElementValue("mobile", txtMobile.Text);` like this you can update other also.. do some learning on this from msdn. if you are confused about the processing of these statement and search on stack overflow there you will get fine example with problem and solution. – Niranjan Singh Apr 03 '12 at 08:06
  • sorry... it didn't worked for me :( foreach (items) { item.SetElementValue("add", txtBox2.Text); item.SetElementValue("mobile", txtBox3.Text); } – gsvirdi Apr 03 '12 at 09:47
  • Error Updating the xml file. A small change in the code `Value == nameComboBox.Text` Everytime only the 1st entry in `` of the xml file is changed according to the Submitted form data. This is creating two records with a same name. The dropdownlist is not showing the duplicate name, but when u select that name then two different types of datas are fetched and displayed one after the other in a blink. – gsvirdi Apr 03 '12 at 09:53
  • you are updating single row at a time.. so it is good if you use `item.SetElementValue("add", "New New Address");`. as per my prediction about your form, you are updating a single patient detail. so follow this, it should work for you rather than looping through elements. – Niranjan Singh Apr 03 '12 at 11:00
  • Done, thx @Niranjan Kala so much for the gr8 piece of code & help :) – gsvirdi Apr 03 '12 at 12:04
1

I would take the xml serialization/deserialization route to solve this:

http://support.microsoft.com/kb/815813

How to Deserialize XML document

That way you can work with objects and not have to parse xml files manually.

Community
  • 1
  • 1
TGH
  • 38,769
  • 12
  • 102
  • 135
  • However that's the industry standard way. School projects sometimes have different requirements :-) – TGH Apr 03 '12 at 04:42
1

If you're using .NET 3.5 onward you can use the XDocument class like the following. I'm assuming your content is in a .xml file.

XDocument xdoc = XDocument.Load(@"C:\Tmp\test.xml");
//this would ensure you get the right node and set its text content/value
xdoc.Element("patients")
     .Element("patient").Element("add").Value = "some new address?";
xdoc.Save(@"C:\Tmp\test.xml");

The file test.xml would change to:

<patients>
 <patient>
  <regNo>2012/Mar/003</regNo>
  <name>Jhon</name>
  <add>some new address?</add>
  <mobile>0000</mobile>
  <stay>2</stay>
  <costofroom>100</costofroom>
  <total>200</total>
 </patient>
</patients>
gideon
  • 19,329
  • 11
  • 72
  • 113
  • Dear gideon, I've tried this solution, very easy to understand, but the problem is that the search function is missing. I need to update all records of Jhon in the xml database. so logically after I fetch records of Jhon, I have to over-write them with the current values present in textbox of my winForm. I was searching for that logic which will remember sibling Nodes of Jhon. Layout of Xml file u showed is correct, but I have many patient details in the xml file. – gsvirdi Apr 03 '12 at 10:00