我正在try 反序列化从服务获取的XMLIM. 但XmlSerializer在遇到第一个属性时失败.
我的缩短的XML如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:basicInformation xmlns:ns2="http://fu.bar.com/">
<somenode>...</somenode>
...
</ns2:basicInformation>
我的班级看起来像这样
[XmlRoot(ElementName = "basicInformation")]
public class BasicInformation
{
[XmlElement(ElementName = "somenode")]
public string SomeNode { get; set; }
...
[XmlAttribute(AttributeName = "ns2")]
public string Ns2 { get; set; }
[XmlText]
public string Text { get; set; }
}
我得到的错误是:
"There is an error in XML document (1, 57)."}
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233079
HelpLink: null
InnerException: {"<basicInformation xmlns='http://message.async.dms.svap.deere.com/'> was not expected."}
Message: "There is an error in XML document (1, 57)."
Source: "System.Xml"
StackTrace: " at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)\r\n at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)\r\n at AzureQueue.Program.Blurg() in E:\\_Code_\\Testing\\AzureQueue\\AzureQueue\\Program.cs:line 432\r\n at AzureQueue.Program.<DoSomething>d__1.MoveNext() in E:\\_Code_\\Testing\\AzureQueue\\AzureQueue\\Program.cs:line 71"
TargetSite: {System.Object Deserialize(System.Xml.XmlReader, System.String, System.Xml.Serialization.XmlDeserializationEvents)}
如果我将XAML中的第二行替换为:
<ns2:basicInformation xmlns:ns2="http://fu.bar.com/">
对此:
<basicInformation>
一切都很正常.
所以这是我没有正确处理的那个属性的东西,但我还没有找到解决方案.
我应该提到的是,C#类是从xml到c#工具生成的,因为完整的xml是巨大的.
那么我错过了什么呢?我可以用非常难看的方法解决这个问题,然后替换一个字符串,但这感觉非常错误!;)
Update
我已经接受了@Marc Gravell的回答,它奏效了. 但我 Select 使用@Denis Michel解决方案,因为它解决了我的问题,而不必在我的c#模型中的所有XML node 上放置名称空间