Question

I am doing the program in convert XML file to List Objects. I have successfully done serialization from List to XML .but I have an problem on doing deserialization. Please anyone tell me what's the wrong i have done in this code.

This is my XML code.

<?xml version="1.0"?>
<Contact_x0020_Form xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Contact>
    <Contact>
      <Id>1</Id>
      <Name>vicky1kumar</Name>
      <Phone>248847227</Phone>
    </Contact>
    <Contact>
      <Id>2</Id>
      <Name>vicky1kumar2kumar</Name>
      <Phone>725228355</Phone>
    </Contact>
    <Contact>
      <Id>3</Id>
      <Name>vicky1kumar2kumar3kumar</Name>
      <Phone>2032848116</Phone>
    </Contact>
    <Contact>
      <Id>4</Id>
      <Name>vicky1kumar2kumar3kumar4kumar</Name>
      <Phone>853938969</Phone>
    </Contact>
    <Contact>
      <Id>5</Id>
      <Name>vicky1kumar2kumar3kumar4kumar5kumar</Name>
      <Phone>530646891</Phone>
    </Contact>
  </Contact>
  <Id>0</Id>
</Contact_x0020_Form>

This is my Class for convert XML to List Object

public class Converter
{
    public static T XmlToObject<T>(string xml)
    {
        using (var xmlStream = new StringReader(xml))
        {
            var serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(XmlReader.Create(xmlStream));
        }
    }

    public static List<T> XmlToObjectList<T>(string xml, string nodePath)
    {
        var xmlDocument = new XmlDocument();
        xmlDocument.LoadXml(xml);

        var returnItemsList = new List<T>();

        foreach (XmlNode xmlNode in xmlDocument.SelectNodes(nodePath))
        {
            returnItemsList.Add(XmlToObject<T>(xmlNode.OuterXml));
        }
        return returnItemsList;
    }
}  

And this is my DEserialization code...

List<string> decont = new List<string>();
decont  = Converter.XmlToObjectList<string>(@"C:\vignesh\serialization\xmlserialize\XmlSerializeContact.xml","//Contact");
                    foreach (var item in decont)
                    {
                        Console.WriteLine(decont);
                    }  

I got this error:

Data at the root level is invalid. Line 1, position 1.

Was it helpful?

Solution 2

First, your xml should look like this:

<?xml version="1.0"?>
<Contact_x0020_Form>
  <Contacts>
    <Contact>
      <Id>1</Id>
      <Name>vicky1kumar</Name>
      <Phone>248847227</Phone>
    </Contact>
    <Contact>
      <Id>2</Id>
      <Name>vicky1kumar2kumar</Name>
      <Phone>725228355</Phone>
    </Contact>
  </Contacts>
</Contact_x0020_Form>

Second, define serialization classes something like :

[XmlRoot(ElementName = "Contact_x0020_Form")]
public class Root
{
    [XmlElement("Contacts")]
    public Contacts contacts{get;set;}
}


public class Contacts
{
  public List<Contact> contacts {get;set;}
}

public class Contact
{
[XmlElement("Id")]
public int Id {get;set;}
[XmlElement("Name")]
public string Name {get;set;}
[XmlElement("Phone")]
public string Phone {get;set;}
}

and your helper classes

public static class serialize
{
public static T Deserialize<T>(string path)
        {
            T result;
            using (var stream = File.Open(path, FileMode.Open))
            {
                result = Deserialize<T>(stream);
            }
            return result;
        }

public static void Serialize<T>(T root, string path)
        {
            using (var stream = File.Open(path, FileMode.Create))
            {
                var xmlSerializer = new XmlSerializer(typeof(T));
                xmlSerializer.Serialize(stream, root);
            }
        }

public static T Deserialize<T>(Stream stream)
        {

            var xmlSerializer = new XmlSerializer(typeof(T));
            return (T)xmlSerializer.Deserialize(stream);
        }
}

And, finally your func:

static void Main()
    {
        var a = serialize.Deserialize<Root>("input.xml"); //xml file name given here.
        Console.WriteLine(a.contacts);
    }

This is how you should proceed. Then on, you can get the list of objects that you want.

OTHER TIPS

Data at the root level is invalid. Line 1, position 1.

To address this first error, you must understand the cause. The issue is LoadXml accepts an xml string; whereas you are passing a path to an Xml file. You need to use Load instead of LoadXml.

That said, there are a lot of other things you need to correct. The serialized XML provided in your question seems to be incorrect--e.g. the Contact node is its own parent. Thus, your node selection is giving you the entire Xml. (Did you mean to define Contacts node to be the parent of the Contact list?)

<Contacts>
.. <Contact>

use this before calling xml data in deserialization

xml = Regex.Replace(xml, "<\\?xml.*?>", String.Empty);

here xml is your xml data.

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