2

I have a "xml" file which looks like this:

<root>
 <a>
  <b>
  </b>
 </a>
</root>
<root>
 <a>
  <b>
  </b>
 </a>
</root>
<root>
 <a>
  <b>
  </b>
 </a>
</root>
....

which is created with the serialize method over a list of objects. So i have the matching class to deserialize it.

Now when i try to deserialize i get the error of multiple roots. Is there a way to deserialize this file in to a list of objects again?

One idea of mine would be to wrap another class around the listed classes and call it something like "rootclass" and than serialize only this one. This would than lead to a single root. But is there anotehr way of doing it with only the given XML file?

my deserialization looks like this:

static public List<CT> DeSerialize(string FileName)
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(List<CT>), new XmlRootAttribute("root"));
        List<CT> result;

        using (FileStream fileStream = new FileStream(@FileName, FileMode.Open))
        {
            result = (List<CT>)deserializer.Deserialize(fileStream);
        }

        return result;
    }
Hunselfunsel
  • 329
  • 1
  • 3
  • 15
  • 2
    `List` and `Array` create their own root object when serialized. Why don't you pass your List/Array object to your serializer? – dotNET Dec 10 '17 at 12:42
  • https://stackoverflow.com/questions/134494/is-there-any-difference-between-valid-xml-and-well-formed-xml/ you may benefit from this post – FakeCaleb Dec 10 '17 at 12:46
  • during the lifetime of the programm i want to save new elements to the file. so i dont really want a real root element – Hunselfunsel Dec 10 '17 at 12:46
  • 1
    " So i have the matching class to deserialize it" As you allready said, there is no "the root" in your case and thus there´s nothing to be deserialized into. This is because your xml isn´t well-formed, which assumes you have exactly *one* root.element. – MakePeaceGreatAgain Dec 10 '17 at 13:08
  • Looks like a duplicate of [Read nodes of a xml file in C#](https://stackoverflow.com/q/46476119/3744182). Agree? – dbc Dec 10 '17 at 15:35

1 Answers1

0

You have fragments so use XmlReader with setting set to fragments :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            XmlReader reader = XmlReader.Create(FILENAME,settings);


            while (!reader.EOF)
            {
                if (reader.Name != "root")
                {
                    reader.ReadToFollowing("root");
                }
                if (!reader.EOF)
                {
                    XElement root = (XElement)XElement.ReadFrom(reader);
                }
            }
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20