【发布时间】:2015-06-28 19:17:25
【问题描述】:
我在反序列化以下 XML 片段(来自 OneNote)时遇到问题:
<one:OE creationTime="2015-03-21T18:32:38.000Z" lastModifiedTime="2015-03-21T18:32:38.000Z" objectID="{649CA68C-C596-4F89-9885-1553A953529E}{30}{B0}" alignment="left" quickStyleIndex="1" selected="partial">
<one:List>
<one:Bullet bullet="2" fontSize="11.0" />
</one:List>
<one:T><![CDATA[Bullet point one]]></one:T>
</one:OE>
以下代码用于反序列化上述片段。 OE 类具有以下属性:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34230")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://schemas.microsoft.com/office/onenote/2013/onenote")]
[System.Xml.Serialization.XmlRootAttribute("OE", Namespace = "http://schemas.microsoft.com/office/onenote/2013/onenote", IsNullable = true)]
public partial class OE : EntityBase<OE>
{
...
}
反序列化片段的实际方法是在基类EntityBase中:
public static T Deserialize(string xml)
{
System.IO.StringReader stringReader = null;
try
{
stringReader = new System.IO.StringReader(xml);
return ((T)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
}
finally
{
if ((stringReader != null))
{
stringReader.Dispose();
}
}
}
反序列化方法调用如下:
var element = OE.Deserialize(xmlString);
其中变量xmlString 是上面给出的XML 片段。在调用Deserialize 方法时,我收到以下错误:
There is an error in XML document (1,2). ---> System.Xml.XmlException: 'one' is an undeclared prefix. Line 1, position 2.
我花了一些时间查看在 OE 类中声明命名空间的属性,但一切似乎都是正确的。谁能指出我犯的错误?
【问题讨论】:
-
错误告诉您确切的问题:命名空间
one未定义。 IE。不存在启动one的 xmlns 属性。 -
由于缺少命名空间声明,我认为这只是从较大的 XML 文件中截取的?是否以编程方式完成?如果是这样,请显示此代码 - 它需要更改为可识别 XML。蛮力砍掉这个元素会导致无效的 XML。
标签: c# xml deserialization xml-deserialization