反序列化包含命名空间的XML元素

时间:2013-04-29 18:26:41

标签: xml xml-namespaces xml-deserialization xsd2code

我收到了一个XSD架构,我使用xsd2code从中创建了对象。

下一步是使用上面生成的对象反序列化示例XML文件。

问题是当我尝试反序列化对象时,单个元素的命名空间存在问题。

我现在正在努力解决这个问题两天,我想知道在哪里看。什么可能是一个问题。

XML成功反序列化元素

<Order xmlns="" id="97440">

但是当它看起来像

时无法反序列化元素
<Order xmlns="http://type.domain.com"  id="97440">

什么可能导致该元素Order不接受任何命名空间?

如果我想在VS中手动编辑XML文件,并生成一个新的Order元素,它将使用空命名空间生成

c#c​​lass包含

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://type.domain.com")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://type.domain.com", IsNullable = false)]

1 个答案:

答案 0 :(得分:3)

约翰,

你需要在C#类

中使用这样的东西
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "http://type.domain.com")]
public class Order
{
    [XmlAttribute("id")]
    public string Id { get; set; }
    [XmlElement(Namespace ="http://www.cohowinery.com")]
    public decimal Price;  
}

您也可以为XmlElement设置命名空间。

    [XmlElement(Namespace ="http://www.cohowinery.com")]
    public decimal Price;  

最好的问候。