Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: RayLopez99 <raylopez88@-----.--->
To: NULL
Date: 7/19/2009 10:02:00 PM
Is there an easy way to do this?  Without using style sheet filters,
etc?  I have a decorated class (uses [XmlRoot], [XmlAttribute], etc)
but I can only add one node at a time when I create the XML file--but
I want to add more than one node, however I get an error if you add
more than one node (error: cannot form XML).

If there's a short way to fix this please let me know.  I think I can
fix it using DOM and the "long  or hard way", by writing and reading
each node individually from a List (I've done this before).  But I'm
trying to do this the "short or easy way" using XML Serialization.

Thank you

RL

//Here is the code: self explanitory, two Decorated class nodes being
added, myDecPerson1 ("Rob1 Smith") and myDecPerson1 ("Rob2 Smith2")
// Discussion on stripping out top element here:  //
http://www.csharper.net/blog/serializing_without_the_namespace__xmlns__xmlns_xsd__xmlns_xsi_.aspx

 protected void cmd_Button1_Click(Object sender, EventArgs e)
        {
            DecoratedPerson myDecPerson1 = new DecoratedPerson();
            myDecPerson1.FirstName = "Rob1";
            myDecPerson1.LastName = "Smith1";
            myDecPerson1.Password = "SecretSmith";
            myDecPerson1.Email = "Smith1@a...";
            myDecPerson1.Age = int.Parse("99");
            myDecPerson1.UserId = "Rob1Smith";
            Guid myGuid = new Guid();
            myGuid = System.Guid.NewGuid();
            myDecPerson1.UserGuid = myGuid;
            // add to List
            myDecPersonList.Add(myDecPerson1);

            // now do second person, and add to list--fails!...

            DecoratedPerson myDecPerson2 = new DecoratedPerson();

            myDecPerson2.FirstName = "Rob2";
            myDecPerson2.LastName = "Smith2";
            myDecPerson2.Password = "SecretSmith2";
            myDecPerson2.Email = "Smith2@a...";
            myDecPerson2.Age = int.Parse("88");
            myDecPerson2.UserId = "Rob2Smith";
            myGuid = new Guid();
            myGuid = System.Guid.NewGuid();
            myDecPerson2.UserGuid = myGuid;
            myDecPersonList.Add(myDecPerson2);

            //create file and add nodes ..

            string totalFilepath = Path.Combine
(Request.PhysicalApplicationPath, @"App_Data
\XMLPasswordDoc77.xml");  //create path
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces
();

            ns.Add("mytest", "http://www.w3.org/2001/XMLSchemaMyOwn");

             XmlWriterSettings settings = new XmlWriterSettings();
             settings.OmitXmlDeclaration = false; // Don't remove the
<?xml version="1.0" encoding="utf-8"?>

             XmlWriter writer = XmlWriter.Create(totalFilepath,
settings);


            XmlSerializer serializer = new XmlSerializer(typeof
(DecoratedPerson));

            foreach (DecoratedPerson p in myDecPersonList)
            {

                 serializer.Serialize(writer, p, ns); //only works w/o
exception if only one element in list! if > 1 fails!
            }

            writer.Close();




        } //end of method


//OUTPUT (works if only one node in List):


//
  <?xml version="1.0" encoding="utf-8" ?>
- <MyEmployee xmlns:mytest="http://www.w3.org/2001/XMLSchemaMyOwn"
GUID="665ae831-275e-427b-9737-9f3856c8c84a">
  <FiRstName>Rob1</FiRstName>
  <lAsTNaMe>Smith1</lAsTNaMe>
  <pAssworD>SecretSmith</pAssworD>
  <eeemaiL>Smith1@a...</eeemaiL>
  <Age>99</Age>
  <UserId>Rob1Smith</UserId>
  </MyEmployee>



// but error if you add more than one node (cannot form XML)

//Here is the decorated class...
[XmlRoot(ElementName="MyEmployee")]
    public class DecoratedPerson
    {
        private string firstName;
        private string lastName;
        private string password;
        private string email;
        private int age;
        private Guid userGuid;
        private string userId;

        [XmlElement(ElementName="FiRstName")]
        public string FirstName
        {
            get
            {
                return firstName;

            }
            set
            {
                firstName = value;
            }
        }

        [XmlElement(ElementName="lAsTNaMe")]
        public string LastName
        {
            get
            {
                return lastName;

            }
            set
            {
                lastName = value;
            }
        }

        [XmlElement(ElementName = "pAssworD")]
        public string Password
        {
            get
            {
                return password;

            }
            set
            {
                password = value;
            }
        }

        [XmlElement(ElementName = "eeemaiL")]
        public string Email
        {
            get
            {
                return email;

            }
            set
            {
                email = value;
            }
        }

        [XmlElement(IsNullable=false)]
        public int Age
        {
            get
            {
                return age;

            }
            set
            {
                age = value;
            }
        }

        [XmlAttribute(AttributeName="GUID")]
        public Guid UserGuid
        {
            get
            {
                return userGuid;

            }
            set
            {
                userGuid = value;
            }
        }

        [XmlElement(IsNullable = true)]
        public string UserId
        {
            get
            {
                return userId;

            }
            set
            {
                userId = value;
            }
        }


        public DecoratedPerson()
        {

        }
    }


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