Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I'm currently dealing with an XML file structured like this:

XML
<contatti>
    <sede>
        <name></name>					
    	<address></address>
	<tel></tel>
	<fax></fax>
	<email></email>
	<link></link>
	<iva></iva>
	<reg></reg>
	<timing></timing>
        <contacts>
                <contact>
		<surname></surname>
		<name></name>
		<area></area>
		<role></role>
		<tel></tel>
		<email></email>
        	</contact>
        </contacts>
    </sede>
</contatti>


To serialize its data I use this class:

C#
{
    [XmlRoot("contatti")]
    public class Contatti
    {
        [XmlElement("sede")]
        public Sede[] ListaSedi { get; set; }

    }

    public class Sede
    {
        [XmlElement("name")]
        public string Name { get; set; }

        [XmlElement("address")]
        public string Address { get; set; }

        [XmlElement("tel")]
        public string Tel { get; set; }

        [XmlElement("fax")]
        public string Fax { get; set; }

        [XmlElement("email")]
        public string Email { get; set; }

        [XmlElement("link")]
        public string Link { get; set; }

        [XmlElement("iva")]
        public string Iva { get; set; }

        [XmlElement("reg")]
        public string Reg { get; set; }

        [XmlElement("timing")]
        public string Timing { get; set; }

        [XmlArray("contacts"), XmlArrayItem("contact")]
        public Contatto[] ListaContatti { get; set; }
    }

    [XmlRoot("contact")]
    public class Contatto
    {
        [XmlElement("surname")]
        public string Surname { get; set; }

        [XmlElement("name")]
        public string Name { get; set; }

        [XmlElement("area")]
        public string Area { get; set; }

        [XmlElement("role")]
        public string Role { get; set; }

        [XmlElement("tel")]
        public string Tel { get; set; }

        [XmlElement("email")]
        public string email { get; set; }
    }
}


The problem is, when I launch the app it returns an exception related to the serializer:
"System.InvalidOperationException: There is an error in XML document (159,8). --> System.Xml.XmlException: ReadElementContentAs() methods cannot be called on an element that has child elements. Line 159, position 8."

So, what do I do? I checked the 159 line but I've didn't noticed anything strange.
Here's a screenshot https://onedrive.live.com/redir?resid=69223EABCE34E2E5!17711&authkey=!AAK0_nwaieS7H-Y&v=3&ithint=photo%2cpng[^]

Someone has any clues?

Thank you to everyone!
Posted

1 solution

I can't see the screen shot, but here are some questions for you that might help your trouble shooting:

1. Is it when you de-serialize the data that the error occurs?
If so, have you tried to open the document in an XML viewer? Maybe you will get some hints
if an error occur there too.

2. Have you checked line 158? (Sometimes it is the line before that fails)

3. Is it the first time a specific node type is processed, e.g. <fax> or <tel>?
If not, what is the difference from a node of that type that works?

Difficult to give more help without more info about the actual XML file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900