我正在制作Windows Phone 8.1 Silverlight应用程序。我想要 LinkedIn身份验证。我从互联网上得到了1-3个样本。但他们都在同一行报告错误:
using System.Xml.Serialization;
personXerializer = new XmlSerializer(typeof(SampleLinkedApp.Data.person));
给我错误:
System.Xml.Serialization.ni.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理 附加信息:反映类型的错误 'SampleLinkedApp.Data.person'。
另外, 的 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; }
}
问题出在哪里?
同时在此问题中添加内部异常:
{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)}
答案 0 :(得分:2)
它告诉你......
来自命名空间''的XML元素'标题'已经存在于当前范围内。使用XML属性为元素指定另一个XML名称或命名空间。
所以看看:
[XmlElement("headline")]
public string Headline { get; set; }
[XmlElement("headline")]
public string Interests { get; set; }
他们不能两者都是<headline>
,因为在反序列化时,无法知道在查看<headline>foo</headline>
时使用哪个。现在,您可以争辩“但我只想序列化”,但XmlSerializer
不信任人们以这种方式工作:它只会序列化它认为也可以反序列化的模型。
猜测,你需要类似的东西:
[XmlElement("interests")]
public string Interests { get; set; }