Milan Mendpara
Milan Mendpara

Reputation: 3131

XML Deserialization without specifying XmlRootAttribute

here is my xml :

<connections total="2" >
       <person>
              <id>ohKiUAZWz2</id>
              <first-name>ミ★нιяαℓ</first-name>
              <last-name>§|-|ä|-|»♥«</last-name>
              <headline>--</headline>
            </person>
       <person>
              <id>LmgYe-Nl2a</id>
              <first-name>kunal</first-name>
              <last-name>b</last-name>
              <headline>Student at MscIT,Surat</headline>               
          </person>
</connection>

from code behind :

List<LinkWall> LinkWallList = new List<LinkWall>();
            XmlNodeList xmlnode = doc.GetElementsByTagName("person");

            foreach (XmlElement ele in xmlnode)
            {

                XmlRootAttribute xr = new XmlRootAttribute("person");

                XmlSerializer mySerializer = new XmlSerializer(typeof(LinkWall),xr);

                StringReader re = new StringReader(ele.InnerXml);

                LinkWallList.Add((LinkWall)mySerializer.Deserialize(re));
            }

here is my class definition :

[XmlRoot("person")]
    public class LinkWall
    {
        public LinkWall()
        { }

        [XmlElement(ElementName =  "id")]
        public string id { get; set; }

        [XmlElement(ElementName = "first-name")]
        public string firstName { get; set; }

        [XmlElement(ElementName = "last-name")]
        public string lastName { get; set; }

        [XmlElement(ElementName = "headline", IsNullable=true)]
        public string headline { get; set; }  
    }

but when i try to deserialize. it show me error like : {"There are multiple root elements."}

is there any solution or alternative for specifying XmlRootAttribute ?

thanks in advance, Milan Mendpara

Upvotes: 2

Views: 3079

Answers (3)

Chintan
Chintan

Reputation: 2808

Try OuterXml instead of InnerXML

StringReader re = new StringReader(ele.OuterXml);

Upvotes: 2

Fjodr
Fjodr

Reputation: 923

I believe you should make a class structured like your xml file and deserialize your xml file into an instance of this class.

MyClass myObject = new MyClass;
XmlSerializer ser = new XmlSerializer(myObject.GetType());
using (FileStream fs = new FileStream(FilePath, FileMode.Open))
{
    XmlTextReader reader = new XmlTextReader(fs);
    myObject = (MyClass)ser.Deserialize(reader);
}

This code runs faster and then you will be able to do wathever you want with the data inside your object.

Upvotes: 1

dash
dash

Reputation: 91510

I think your issue is with this line:

StringReader re = new StringReader(ele.InnerXml);

Change it to:

StringReader re = new StringReader(ele.OuterXml);

The reason is the InnerXml property will return all of the child nodes but not the parent node. OuterXml will include your parent person node too.

i.e. InnerXml has no root element (well, it has many!):

<id>ohKiUAZWz2</id>
<first-name>?????al</first-name>
<last-name>§|-|ä|-|»?«</last-name>
<headline>--</headline>

OuterXml is as expected:

<person>
    <id>ohKiUAZWz2</id>
    <first-name>?????al</first-name>
    <last-name>§|-|ä|-|»?«</last-name>
    <headline>--</headline>
</person> 

There is also no real need to use the XmlSerializer constructor you are using. Try:

XmlSerializer mySerializer = new XmlSerializer(typeof(LinkWall));

Instead.

Upvotes: 5

Related Questions