反序列化Xml文档错误

时间:2013-11-25 11:54:18

标签: c# xml xml-deserialization

当我将xml反序列化为对象列表时,我遇到了问题。我今天早上在网上搜索了,但我的问题没有解决。

反序列化方法

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);
        }

    }  

通话方法

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;  

我在这里调用它的原因是我想再次将此对象添加到xml文件中。

错误是这是XML文档中的错误(15,27)。

我的Xml结构

<?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>

1 个答案:

答案 0 :(得分:0)

我从上面的代码中了解到,您尝试扩展当前的XML,首先将其作为FileStream阅读,然后使用XmlWriter为其添加更多内容。< / p>

如果我的理解是正确的,那么您正在尝试写入现有XML文件的末尾,这是不允许的,因为任何XML文档只能有一个根节点。在您的情况下,该根节点为ArrayOfSerializeClass

因此,为了成功完成您的任务,您必须在根节点中附加XML。

<强>更新

此处可能的解决方案:how to append a xml file in c#?