【问题标题】:'System.InvalidOperationException' occurred in System.Xml.Serialization.ni.dllSystem.Xml.Serialization.ni.dll 中发生“System.InvalidOperationException”
【发布时间】:2014-12-30 10:14:17
【问题描述】:

我正在制作一个 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)}

【问题讨论】:

标签: c# xml windows-phone-8.1 linkedin xmlserializer


【解决方案1】:

它告诉你...

命名空间“”中的 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; }

【讨论】:

  • 如果你想要两个同名的元素,this answer 帮了我,它使用了一个列表。
猜你喜欢
  • 2015-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多