开发者

C# XMLdocument writing same thing over again

In my application, lots of data is generated and then written in a XML file using XmlDocument. The mechanics works fine, but when I call the function that serializes again and again to write data in a new XML, it copies what it wrote previously to the new XML plus the new data!

I don't understand, I'm calling XmlDocument doc = new XmlDocument() at the beggining of the function that serializes...

        private static Boolean SaveParsedDataAsXML(ParsedData DataParsed, String Name)
        {
            try
            {
                XmlDocument doc = new XmlDocument();

                XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                doc.AppendChild(xmlnode);

                XmlElement generalVariables = doc.CreateElement("Variables");

                generalVariables.AppendChild(SerializeElement(doc, "Path", DataParsed.Path));
. 
.
.
            XmlElement chatMessages = doc.CreateElement("ChatMessages");
            foreach (Message mess in DataParsed.ChatMessages)
            {
                XmlElement singleMess = doc.CreateElement("SingleMessage");

                singleMess.AppendChild(SerializeElement(doc, "MessageID", mess.MessageID.ToString()));
                singleMess.AppendChild(SerializeEl开发者_运维问答ement(doc, "MessageName", mess.MessageName));
                singleMess.AppendChild(SerializeElement(doc, "MessageTime", mess.MessageTime.ToString()));
                singleMess.AppendChild(SerializeElement(doc, "MessageContent", mess.MessageContent));
                singleMess.AppendChild(SerializeElement(doc, "MessageTarget", mess.MessageTarget.ToString()));

                chatMessages.AppendChild(singleMess);
            }


           generalVariables.AppendChild(chatMessages);
.
.
.
                doc.AppendChild(generalVariables);

                //Saving and returning true, serialization successful.
                doc.Save(OutputPath + "\\" + ReplayName + ".xml");               

                return true;
            }
            catch
            {
                return false;
            }
        }


My assumption would be that you're not clearing the DataParsed.ChatMessages object before you pass it in to the function you posted.

Are you sure you're using a new DataParsed object each time? Or are you re-using the same instance and appending more messages to it?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜