Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,
I am storing some information in a xml file on a button click event,when i am generating a xml file am getting an error "Only one top level element tag is possible". Is it possible to have one top level element tag on another.Please help

XML Code on button click

C#
FileStream fStream = new FileStream("D:\\Test.xml", FileMode.Open);
           XmlRootAttribute xroot = new XmlRootAttribute();
           xroot.ElementName = "Root";
           xroot.Namespace = "D:\\Test.xml";
           xroot.IsNullable = true;
           foreach (DictionaryEntry item in PropertyList)
           {
              XmlSerializer xml = new XmlSerializer(item.Value.GetType(),xroot);
              xml.Serialize(fStream, item.Value);
           }
Posted
Updated 14-Jul-11 23:40pm
v4
Comments
Wayne Gaylard 15-Jul-11 5:40am    
Added PRE tags.

Why are you trying to recreate the Root? you don't really need to do that. Just do something like this

C#
XmlSerializer xmlFormat = new XmlSerializer(item.Value.GetType());
            using (Stream fs = new FileStream(@"D:\Test.xml", FileMode.Open, FileAccess.Write, FileShare.None))
            {
                xmlFormat.Serialize(fs, item.Value);
            }


That will append the serialised object to the stated file. If you are unsure whether the file has been created yet just use

C#
using(Stream fs = new FileStream(fileName, FileMode.OpenOrCreate,FileAccess.Write,FileShare.None))


instead, and that will create the file if it does not exist.

Hope this helps
 
Share this answer
 
Without XML or code, we cannot point your mistake. But have a look at this. Check your code and XML.
http://forums.asp.net/t/1062792.aspx/1[^]
 
Share this answer
 
Comments
vajisha 15-Jul-11 5:16am    
i have given u the xml code
You're trying to create new serializer for each item and then use it to serialize objects into the same stream using the same root element. Looks like Serialize method attempts to append newly serialized data to the stream, writing root elements for each cycle run. Consider moving
XmlSerializer xml = new XmlSerializer(item.Value.GetType(),xroot);

line outside the loop and changing constructor to another, supporting an array of types (you may fill it statically or dynamically, using another cycle). Or write data without header to the memory stream and then flush it to file :)
 
Share this answer
 
As per Wayne Gaylard's Solution i have written the followiong code but still it is showing me the same error
FileStream fStream = new FileStream("D:\\Test.xml", FileMode.OpenOrCreate,FileAccess.Write,FileShare.Read);
            foreach (DictionaryEntry item in PropertyList)
            {
                XmlSerializer xml = new XmlSerializer(item.Value.GetType());
                xml.Serialize(fStream, item.Value);
            }
MessageBox.Show("SAVED");
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900