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/23/2009 7:02:00 AM
On Jul 23, 5:56=A0am, Martin Honnen <mahotr...@yahoo.de> wrote:
> For me that sample successfully serializes and deserializes so I have no
> idea why you say it contains a mistake and does not find "Foo" elements.
>
>

Hello Martin,

I tried your program in Windows Console mode and it works, but I=92m
afraid it does not work for my more complicated class object being de-
serialized.

If you have any ideas why please post.  Please don=92t spend too much
time as you have helped enough.

I personally don=92t care that I cannot get my program to work like
yours, even though it would be an improvement, since my old, long way
of checking each element, and de-serialising that element, though more
time consuming, does work for me.  However, it would be nice to
deserialize the entire object =93at once=94, like is done in binary
deserialization.

//Here is the relevant code for deserializing.  It does not work,
since the condition xr.Name =3D=3D =93DecoratedPerson=94 is never reached.

This code fragment appears to be failing: =93if(xr.NodeType =3D=3D
XmlNodeType.Element && xr.Name =3D=3D "DecoratedPerson")=94

When I run the Debugger, the condition xr.Name =3D=3D =93DecoratedPerson=94=
 is
never reached, because xr.Name is:  =93FiRstName=94 or =93theEmaiL=94 or =
=93Age=94
or =93UserId=94, etc. It is never "=3D=3DDecoratedPerson"

Consequently, the List is never populated and always has a count of
zero, being empty.

Thank you for your help.

RL

//Here is my =93class object=94, called DecoratedPerson, that is
serialized in XML.  I am attempting to de-serialize it with the code
below.

  <?xml version=3D"1.0" encoding=3D"utf-8" ?>
- <ARootElement>
- <MyEmployee xmlns:mytest=3D"http://www.w3.org/2001/XMLSchemaMyOwn"
GUID=3D"1f6aafa0-61ee-4f76-9935-d308299ff8bf">
  <FiRstName>Rob1</FiRstName>
  <LAsTNaMe>Smith1</LAsTNaMe>
  <thePassword>SecretSmith</thePassword>
  <theEmaiL>Smith1@a...</theEmaiL>
  <Age>19</Age>
  <UserId>Rob1Smith</UserId>
  </MyEmployee>
- <MyEmployee xmlns:mytest=3D"http://www.w3.org/2001/XMLSchemaMyOwn"
GUID=3D"2057ad4f-8511-4ec6-b7be-c9652571e407">
  <FiRstName>Rob2</FiRstName>
  <LAsTNaMe>Smith2</LAsTNaMe>
  <thePassword>SecretSmith2</thePassword>
  <theEmaiL>Smith2@a...</theEmaiL>
  <Age>18</Age>
  <UserId>Rob2Smith</UserId>
  </MyEmployee>
- <MyEmployee xmlns:mytest=3D"http://www.w3.org/2001/XMLSchemaMyOwn"
GUID=3D"325e0a07-329d-4544-ba36-e79abe2cbc6b">
  <FiRstName>Rob3</FiRstName>
  <LAsTNaMe>Smith3</LAsTNaMe>
  <thePassword>SecretSmith3</thePassword>
  <theEmaiL>Smith3@a...</theEmaiL>
  <Age>33</Age>
  <UserId>Rob3Smith</UserId>
  </MyEmployee>
  </ARootElement>

//Here is the relevant code for deserializing the above.  It does not
work, since the condition xr.Name =3D=3D =93DecoratedPerson=94 is never
reached.

This code fragment appears to be failing: =93if(xr.NodeType =3D=3D
XmlNodeType.Element && xr.Name =3D=3D "DecoratedPerson")=94

When I run the Debugger, the condition xr.Name =3D=3D =93DecoratedPerson=94=
 is
never reached, because xr.Name is:  =93FiRstName=94 or =93theEmaiL=94 or =
=93Age=94
or =93UserId=94, etc. It is never "=3D=3DDecoratedPerson"

Consequently, the List is never populated and always has a count of
zero, being empty.

RL

     protected void cmd_Button3_Click(Object sender, EventArgs e)
        {
            string totalFilepath =3D Path.Combine
(Request.PhysicalApplicationPath, @"App_Data
\XMLPasswordDocManyNodes.xml");  //create path
            this.ReadXMLandAddtoDecoratedPersonList2(totalFilepath);
        }

        private string ReadXMLandAddtoDecoratedPersonList2(string
filepath)
        {
            string statusStr =3D "Success
ReadXMLandAddtoDecoratedPersonList2";

            try
            {
                using (XmlReader xr =3D XmlReader.Create(filepath))
                {
                    XmlSerializer ser =3D new XmlSerializer(typeof
(DecoratedPerson));

                    myDecPersonList.Clear();
                    while (xr.Read())
                    {
                        if(xr.NodeType =3D=3D XmlNodeType.Element &&
xr.Name =3D=3D "DecoratedPerson") ///!!!
                        {
                            DecoratedPerson foo =3D (DecoratedPerson)
ser.Deserialize(xr);
                            myDecPersonList.Add(foo);
                        }



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

            finally
            {

                int j =3D myDecPersonList.Count;
                Debug.WriteLine("count is: " + j.ToString());
                foreach (DecoratedPerson p in myDecPersonList)
                {
                    Debug.WriteLine("these values exist for
DecoratedPerson!: " + 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