How to deserialize an XML doc with a prefixed namespace but no ns-prefixed elements?

喜夏-厌秋 提交于 2019-12-01 11:51:51

You have to add the XmlElement attribute to each property as well, setting the Namespace property to an empty string (as the namespace is not inherited in your situation).

Your definition for Info should look like this:

XmlRoot(Namespace = "http://www.ZomboCorp.com/")]
public class Info
{
    [XmlElement(Namespace = "")]
    public String Name { get; set; }
    [XmlElement(Namespace = "")]
    public String Job { get; set; }
}

Then it will deserialize correctly.

I used xsd.exe (a VS tool) and generated a schema from the XML file and then a class file from the schema. It suggested

[XmlType(AnonymousType = true, Namespace = "http://www.ZomboCorp.com/")]
[XmlRoot(Namespace = "http://www.ZomboCorp.com/", IsNullable = false)]
public class Info
{
    [XmlElement(Form = XmlSchemaForm.Unqualified)]
    public String Name { get; set; }
    [XmlElement(Form = XmlSchemaForm.Unqualified)]
    public String Job { get; set; }
}

But, I was able to get away with

[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "http://www.ZomboCorp.com/")]
public class Info
{
    [XmlElement(Form = XmlSchemaForm.Unqualified)]
    public String Name { get; set; }
    [XmlElement(Form = XmlSchemaForm.Unqualified)]
    public String Job { get; set; }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!