Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#XMLWindowsForms
Hi, so I built a windows form application for a MEWS System, which is used in hospitals to monitor a patients health. So i've built the program and it works fine, now what I want to to do is store that data using XML File steamwriter and be able to read that data using the readwriter. The problem is I dont know how to utilize this in my code. Since im only familiar with console apps im not sure how I would go about using this in a form app.
 
Heres my code for the program so far:
 
public partial class frmMews : Form
    {
 
        Random r = new Random();
 
        public frmMews()
        {
            InitializeComponent();
            lblResps.BackColor = System.Drawing.Color.Magenta;
            lblHeart.BackColor = System.Drawing.Color.Cyan;
            lblBlood.BackColor = System.Drawing.Color.Red;
            lblConscious.BackColor = System.Drawing.Color.Green;
            lblTemp.BackColor = System.Drawing.Color.Brown;
            lblUrine.BackColor = System.Drawing.Color.Yellow;
 
        }
 
        private void frmMews_Load(object sender, EventArgs e)
        {
            // Form Load Event

            tmrResps.Enabled = false;
            tmrResps.Interval = 300;  // milliseconds

            tmrHeart.Enabled = false;
            tmrHeart.Interval = 300;  // milliseconds

            tmrBlood.Enabled = false;
            tmrBlood.Interval = 300;  // milliseconds

            tmrConscious.Enabled = false;
            tmrConscious.Interval = 800;  // milliseconds

            tmrTemp.Enabled = false;
            tmrTemp.Interval = 600;  // milliseconds

            tmrUrine.Enabled = false;
            tmrUrine.Interval = 300;  // milliseconds

 
        }
 
        private void btnStart_Click(object sender, EventArgs e)
        {
            // Start Button Event

            tmrResps.Enabled = true;
            tmrResps.Interval = 3000;  // milliseconds

            if (tmrResps.Enabled == true)
            {
                txtRespScore.Text = "1";
            }
            else
            {
               txtRespScore.Text = "3";
               MessageBox.Show("Alert! Patients Respiration Score is 3 or more, urgent medical attention is required");
            }
 
            //---

            tmrHeart.Enabled = true;
            tmrHeart.Interval = 4000;  // milliseconds

            if (tmrHeart.Enabled == true)
            {
                txtHeartScore.Text = "0";
            }
            else
            {
                txtHeartScore.Text = "3";
                MessageBox.Show("Alert! Patients Heart rate Score is 3 or more, urgent medical attention is required");
            }
 

            //---

            tmrBlood.Enabled = true;
            tmrBlood.Interval = 3000;  // milliseconds

            if (tmrBlood.Enabled == true)
            {
                txtBloodScore.Text = "1";
            }
            else
            {
                txtBloodScore.Text = "3";
                MessageBox.Show("Alert! Patients Blood level Score is 3 or more, urgent medical attention is required");
            }
            
            //---

            tmrConscious.Enabled = true;
            tmrConscious.Interval = 1000;  // milliseconds

 
            if (tmrConscious.Enabled == true)
            {
                txtConsciousScore.Text = "1";
            }
            else
            {
                txtConsciousScore.Text = "3";
                MessageBox.Show("Alert! Patients Conscious Score is 3 or more, urgent medical attention is required");
            }
 
            //----

            tmrTemp.Enabled = true;
            tmrTemp.Interval = 3000;  // milliseconds

            txtTempScore.Text = "2";
 
            if (tmrTemp.Enabled == true)
            {
                txtTempScore.Text = "2";
            }
            else
            {
                txtTempScore.Text = "3";
 
                MessageBox.Show("Alert! Patients Temperature Score is 3 or more, urgent medical attention is required");
            }
 
            //---

            tmrUrine.Enabled = true;
            tmrUrine.Interval = 2000;  // milliseconds

            if (tmrUrine.Enabled == true)
            {
                txtUrineScore.Text = "0";
            }
            else
            {
                txtUrineScore.Text = "3";
 
                MessageBox.Show("Alert! Patients Urine output Score is 3 or more, urgent medical attention is required");
            }
 
        }
 
        private void tmrResps_Tick(object sender, EventArgs e)
        {
            Random r = new Random();
 
            int x = ( (int)( (r.Next(0, 100) ) / 10) ) * 10;
 
            //---

            lblResps.Size = new System.Drawing.Size( x, 23);
 

        }
 
        private void tmrHeart_Tick(object sender, EventArgs e)
        {
            int x = ((int)((r.Next(0, 100)) / 10)) * 10;
 
            lblHeart.Size = new System.Drawing.Size(x, 23);
 
        }
 
        private void tmrBlood_Tick(object sender, EventArgs e)
        {
            int x = ((int)((r.Next(0, 100)) / 10)) * 10;
 
            lblBlood.Size = new System.Drawing.Size(x, 23);
 

        }
 
        private void tmrConscious_Tick(object sender, EventArgs e)
        {
            int x = ((int)((r.Next(0, 100)) / 10)) * 10;
 
            lblConscious.Size = new System.Drawing.Size(x, 23);
        }
 
        private void tmrTemp_Tick(object sender, EventArgs e)
        {
            int x = ((int)((r.Next(0, 100)) / 10)) * 10;
 
            lblTemp.Size = new System.Drawing.Size(x, 23);
 
        }
 
        private void tmrUrine_Tick(object sender, EventArgs e)
        {
            int x = ((int)((r.Next(0, 100)) / 10)) * 10;
 
            lblUrine.Size = new System.Drawing.Size(x, 23);
 
        }
 
        private void btnScore_Click(object sender, EventArgs e)
        {
            txtMewsScore.Text = "5";
 

            MessageBox.Show("Alert! Patients MEWS Score is 4 or more, urgent medical attention is required");
 

        }
 
        }
    }
Posted 14-Dec-10 11:05am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

My best advice is using Data Contract and DataContractSerializer with StreamWriter and StreamReader or whatever stream-related class you may want.
 
This is the only unobtrusive way of serialization available in .NET (you can use v.3.5 or v.4.0). Also, it can save and restore any arbitrary object graph, not only object tree or single object. This way is also the best in terms of ease-of-use and supportability. For common-use purposes (and also for remoting, WCF) you can nearlt always consider all other ways as obsolete or inferior.
 
You simply add the attribute [DataContract] to every type involved in the model to be stored/restored; better always use your own name space Web URL through the Namespace attribute parameter (property). Then, apply attribute [DataMember] to every member you want to persist. These members does not even have to be public, or properties; members can be collections, arrays -- virtually no limitation. Apparently, these steps will not modify your class hierarchy, will not modify any behavior -- nothing.
 
Now, if you want to modify your classes, you should be careful to provide incremental behavior with old files, but only for those members which are part of contract (marked with [DataMember]). After all, if have a contract, you're only responsible for contract items. When you simple add more [DataMember] members, your old data remains usable; new members will automatically assume default values.
 
Finally, create you contact serializer based on your top-level object's type:
 
DataContractSerializer ser =
                new DataContractSerializer(typeof(Top));
//...
Top of object graph to store in your stream:
Top top = //...
//...
ser.WriteObject(xmlWriter, top);
//..
Top newGraph = (Top)ser.ReadObject(xmlReader);
 
ReadObject and WriteObject will work with XmlReader/XmlWriter, XmlDictionaryReader/XmlDictionaryWriter, respectively, and any Stream.
 
You can find pretty good documentation and samle code in Microsof help:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx and
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx.
 
Will it help you? Please try and ask further questions.
 
Thank you.
  Permalink  
Comments
Espen Harlinn at 26-Feb-11 10:55am
   
Good reply - a 5
SAKryukov at 26-Feb-11 20:12pm
   
Thank you, also good enough to reuse. --SA
Mika Wendelius at 29-Dec-11 17:56pm
   
5+
SAKryukov at 29-Dec-11 20:22pm
   
Thank you, Mika. --SA

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 685
1 Manas Bhardwaj 380
2 OriginalGriff 344
3 Abhinav S 293
4 Sampath Lokuge 245
0 Sergey Alexandrovich Kryukov 7,967
1 OriginalGriff 4,332
2 Peter Leow 3,699
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 14 Dec 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid