简体   繁体   中英

'System.InvalidOperationException' occurred in System.Xml.Serialization.ni.dll

I am making a Windows Phone 8.1 Silverlight Application. I want LinkedIn Authentication in it. I got 1-3 samples from internet. but they all are reporting error on same line:

using System.Xml.Serialization;
personXerializer = new XmlSerializer(typeof(SampleLinkedApp.Data.person));

Giving me Error:

An exception of type 'System.InvalidOperationException' occurred in System.Xml.Serialization.ni.dll but was not handled in user code Additional information: There was an error reflecting type 'SampleLinkedApp.Data.person'.

Also, person.cs:

[XmlRoot("person")]
    public class person
    {      
        [XmlElement("first-name")]
        public string FirstName { get; set; }

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

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

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

Where Is the problem?


Also adding inner exception to this question:

    {System.InvalidOperationException: There was an error reflecting property 'Interests'. ---> System.InvalidOperationException: The XML element 'headline' from namespace '' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element.
   at System.Xml.Serialization.XmlReflectionImporter.AddUniqueAccessor(INameScope scope, Accessor accessor)
   at System.Xml.Serialization.XmlReflectionImporter.AddUniqueAccessor(MemberMapping member, INameScope elements, INameScope attributes, Boolean isSequence)
   at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)}

It tells you...

The XML element 'headline' from namespace '' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element.

So look at:

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

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

They can't both be <headline> , because when deserializing it would have no way of knowing which to use when seeing <headline>foo</headline> . Now, you could argue "but I only want to serialize", but XmlSerializer doesn't trust folks to work that way: it will only serialize models that it thinks it can also deserialize.

At a guess, you want something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM