Question

I have a problem when I deserialize the xml into List of Objects. I searched it on the net this morning, but my problem isn't resolved.

Deserialization method

public static List<FileAction> DeSerialization()
{
    XmlRootAttribute xRoot=new XmlRootAttribute();
    xRoot.ElementName="ArrayOfSerializeClass";
    xRoot.IsNullable=true;
    XmlSerializer serializer = new XmlSerializer(typeof(List<FileAction>),xRoot);//, new XmlRootAttribute("ArrayOfSerializeClass")

    using (Stream streamReader = File.OpenRead(@"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml"))//FileStream fs =new FileStream(xmlPath,FileMode.Open)
    {
        using (XmlReader reader = XmlReader.Create(streamReader))
        {
           int count =0;

           List<FileAction> serialList2 = (List<FileAction>)serializer.Deserialize(reader);

            return (List<FileAction>)serializer.Deserialize(reader);
        }

    }  

Calling Method

String resultPath = @"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml";
if (!File.Exists(resultPath))
{
    XmlSerializer xs = new XmlSerializer(typeof(List<SerializeClass>));
    using (FileStream fileStream = new FileStream(@"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml", FileMode.Create))
    {
        xs.Serialize(fileStream, serializeList);//seri
        fileStream.Close();
    }
    Console.WriteLine("Succesfully serialized to XML");
}
else
{
    //string path= @"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml";
    DeSerialization();
    XmlSerializer xs = new XmlSerializer(typeof(List<SerializeClass>));
    FileStream fs = new FileStream(@"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml", FileMode.Append, FileAccess.Write);
    using (XmlWriter xwr = XmlWriter.Create(fs))//TextWriter xwr = new StreamWriter
    {
        xs.Serialize(xwr, serializeList);//seri
        //fs.Close();                
    }   
Console.WriteLine("Succesfully serialized to XML");
}
return serializeList;  

The reason why I am calling it here is that I want to add this object again to the xml file.

THe error is that here is an error in XML document (15,27).

My Xml structure

<?xml version="1.0"?>
<ArrayOfSerializeClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SerializeClass>
        <creationTime>2013-11-25T09:53:25.3325289+05:30</creationTime>  
        <fileAction>Renamed</fileAction>
        <Properties>
            <FileAttributes fileName="validate json.txt">
                <fileSize>307</fileSize>
                <extension>.txt</extension>
                <lastAccessTime>2013-11-25T09:53:25.3325289+05:30</lastAccessTime
                <fullPath>C:\serialization\SerializationWithFileWatcher\SerializationWithFileWatcherProj\validate json.txt</fullPath>
            </FileAttributes>
        </Properties>
    </SerializeClass>
</ArrayOfSerializeClass>
Was it helpful?

Solution

What I understand from the code above is that you are trying to extend the current XML, by first reading it as a FileStream and then using an XmlWriter to add some more content to it.

If my understanding is correct, then you are trying to write to the end of an existing XML file, which is not allowed since any XML document can have only one root node. In your case, that root node is ArrayOfSerializeClass.

So, in order to successfully achieve your task, you must append your XML within the root node.

Update:

Possible solution here: how to append a xml file in c#?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top