Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Append to a file using XML Serialization? Easy way to do this?

From: RayLopez99 <raylopez88@-----.--->
To: NULL
Date: 7/20/2009 3:08:00 PM
 Also does anybody know if there is a 'easier' (or cleaner, less code
required) way of deserializing than checking each node as per the
below code?

RL

//as inspired by: http://support.microsoft.com/kb/307548

private string ReadXMLandAddtoDecoratedPersonList(string filepath)
        {
            string statusStr = "Success
ReadXMLandAddtoDecoratedPersonList";

            try
            {
                using (FileStream fs = new FileStream(filepath,
FileMode.Open))
                {
                    using (XmlTextReader r = new XmlTextReader(fs))

                    {
                        myDecPersonList.Clear();

                        DecoratedPerson newDecPerson1 = new
DecoratedPerson();
                        while (r.Read())
                        {
                            Debug.WriteLine("r.Read element is: " +
r.NodeType.ToString() + "(type)" + "," + r.ValueType.ToString() +
" (value type)" + "," + r.Value.ToString() + "(value)" + "," +
r.Name.ToString() + "Name!");
                            if (r.NodeType == XmlNodeType.Element &&
r.Name == "MyEmployee")
                            {

                                string aStringGuid = r.GetAttribute
(1);//gets Second attribute, which is GUID (.GetAttribute(0) is
schema)
                                Debug.WriteLine("this is MyEmployee
attribute: " + aStringGuid);
                                newDecPerson1.UserGuid = new Guid
(aStringGuid);

                            }
                            if (r.NodeType == XmlNodeType.Element &&
r.Name == "FiRstName")
                            {
                                r.Read(); // req'd to push to next
element
                                if (r.NodeType == XmlNodeType.Text)
                                {
                                    newDecPerson1.FirstName = r.Value;
                                    Debug.WriteLine("r.Read element
FirstName is: " + r.NodeType.ToString() + "(type)" + "," +
r.ValueType.ToString() + " (value type)" + "," + r.Value.ToString() +
"(value)");

                                }
                            }

                            if (r.NodeType == XmlNodeType.Element &&
r.Name == "LAsTNaMe")
                            {
                                r.Read(); //!! req'd to push to next
element
                                if (r.NodeType == XmlNodeType.Text)
                                {
                                    newDecPerson1.LastName = r.Value;
                                    Debug.WriteLine("r.Read element
LastName is: " + r.NodeType.ToString() + "(type)" + "," +
r.ValueType.ToString() + " (value type)" + "," + r.Value.ToString() +
"(value)");

                                }
                            }

                            if (r.NodeType == XmlNodeType.Element &&
r.Name == "thePassword")
                            {
                                r.Read(); //!! req'd to push to next
element
                                if (r.NodeType == XmlNodeType.Text)
                                {
                                    newDecPerson1.Password = r.Value;
                                    Debug.WriteLine("r.Read element
Password is: " + r.NodeType.ToString() + "(type)" + "," +
r.ValueType.ToString() + " (value type)" + "," + r.Value.ToString() +
"(value)");

                                }
                            }

                            if (r.NodeType == XmlNodeType.Element &&
r.Name == "theEmaiL")
                            {
                                r.Read(); //!! req'd to push to next
element
                                if (r.NodeType == XmlNodeType.Text)
                                {
                                    newDecPerson1.Email = r.Value;
                                    Debug.WriteLine("r.Read element
Email is: " + r.NodeType.ToString() + "(type)" + "," +
r.ValueType.ToString() + " (value type)" + "," + r.Value.ToString() +
"(value)");

                                }
                            }

                            if (r.NodeType == XmlNodeType.Element &&
r.Name == "Age")
                            {
                                r.Read(); //!! req'd to push to next
element
                                if (r.NodeType == XmlNodeType.Text)
                                {
                                    try
                                    {
                                        newDecPerson1.Age =
Convert.ToInt32(r.Value);
                                        Debug.WriteLine("r.Read
element Age is: " + r.NodeType.ToString() + "(type)" + "," +
r.ValueType.ToString() + " (value type)" + "," + r.Value.ToString() +
"(value)");


                                    }
                                    catch (FormatException)
                                    {
                                        Debug.WriteLine("The Age did
not convert: default zero used");
                                        newDecPerson1.Age = 0;
                                    }


                                }
                            }

                            if (r.NodeType == XmlNodeType.Element &&
r.Name == "UserId")
                            {
                                r.Read(); //!! req'd to push to next
element
                                if (r.NodeType == XmlNodeType.Text)
                                {
                                    newDecPerson1.UserId = r.Value;
                                    Debug.WriteLine("r.Read element
UserId is: " + r.NodeType.ToString() + "(type)" + "," +
r.ValueType.ToString() + " (value type)" + "," + r.Value.ToString() +
"(value)");

                                }
                            }

                            if ((newDecPerson1.FirstName !=
String.Empty) && (newDecPerson1.LastName != String.Empty) &&
(newDecPerson1.Password != String.Empty) && (newDecPerson1.Email !=
String.Empty) && (newDecPerson1.Age != -1) &&  (newDecPerson1.UserId !
= String.Empty))
                            {
                                //add new person to the list and
create a new newDecPerson
                                myDecPersonList.Add(newDecPerson1);
                                Debug.WriteLine("now adding a
DecoratedPerson comprising...");
                                Debug.WriteLine
(newDecPerson1.FirstName + "," + newDecPerson1.LastName + "," +
newDecPerson1.Password + "," + newDecPerson1.Email + "," +
newDecPerson1.Age.ToString() + "," + newDecPerson1.UserId);
                                newDecPerson1 = new DecoratedPerson
(); //re-instantiate a new object, since old one added to List.

                            }


                        }

                    }

                }
            }
            catch (FileNotFoundException fNF)
            {
                Debug.WriteLine(fNF.Message);
                statusStr = fNF.Message;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                statusStr = ex.Message;
            }

            finally
            {
                ////debug output:  works fine, as expected
                foreach (DecoratedPerson p in myDecPersonList)
                {
                    Debug.WriteLine("these values exist for p: " +
p.LastName + "," + p.FirstName + "," + p.Password + "," + p.Email +
"," + p.Age.ToString() + "," + p.UserId + "," + p.UserGuid);
                }

            }

            return statusStr;

        }



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent