简体   繁体   English

XML 反序列化为 xml 属性返回 null,但返回正确的 Xml 文本

[英]XML Deserialization is returning null for an xml attribute but is returning the correct Xml text

Below I have the following objects:下面我有以下对象:

[XmlRoot("ExampleObject")]
public class ExampleObject
{
    [XmlElement("element1")]
    public long element1{ get; set; }

    [XmlElement("element2")]
    public string element2{ get; set; }

    [XmlElement("element3")]
    public string element3{ get; set; }

    [XmlElement("element4")]
    public ExampleObject2 element4{ get; set; }
}

[Serializable()]
public class ExampleObject2
{
    [XmlText]
    public string Value { get; set; }

    [XmlAttribute]
    public string Id { get; set; }

    [XmlAttribute]
    public string Ref { get; set; }   
}

and the following XML:以及以下 XML:

<c:ExampleObject>
   <c:element1>
        ...
   </c:element1>
   <c:element2>
          ...
   </c:element2>
   <c:element3>
          ...
   </c:element3>
   <c:element4 z:Ref="953" i:nil="true"/> <!--- or this <c:Status z:Id="953">...</c:element4> -->
</ExampleObject>

I am able to get my object perfectly fine, except for element4 where inside ExampleObject2 my Id and Ref are null while Value has the correct value inside.我能得到我的对象完全正常,除了element4 ,其中内ExampleObject2我的IdRef是空的,而Value有正确的值内。

I want it so that ExampleObject2 always has a Value , it is either null or the value it is supposed to has.我想要它,以便ExampleObject2总是有一个Value ,它要么是 null 要么是它应该具有的值。 I want Id and Ref to be the value of the attribute inside element4 depending on if element4 has that attribute or not.我希望IdRefelement4内属性的值,具体取决于element4是否具有该属性。

I was wondering if there's something I am missing?我想知道是否有我遗漏的东西?

I have also tried to add specific names for the XMLElement tags like this [XmlAttribute("Id")] but that doesn't seem to do anything.我还尝试为这样的 XMLElement 标签添加特定名称[XmlAttribute("Id")]但这似乎没有任何作用。 I have also tried including the namespace like this [XmlAttribute("z:Id")] but that causes an error.我也试过包括这样的命名空间[XmlAttribute("z:Id")]但这会导致错误。

I have also tried putting IsNullable within the element4 XMLElement tag but for the XML elements that have nil="true" but it makes the entire ExampleObject2 null which isn't exactly what I am looking for.我还尝试将IsNullable放在 element4 XMLElement标记中,但对于具有nil="true"的 XML 元素,但它使整个ExampleObject2空,这并不是我正在寻找的。

I've already looked at the following question and have come to the same issue.我已经看过以下问题并且遇到了同样的问题。

how to deserialize an xml node with a value and an attribute using asp.net serialization 如何使用asp.net序列化反序列化具有值和属性的xml节点

Thank you谢谢

I ended up fixing it with the Nil property like @GeorgeBirbilis said and used it with @dbc's solution to manually naming the namespaces the z: and c: are from.我最终用@GeorgeBirbilis 所说的 Nil 属性修复了它,并将它与@dbc 的解决方案一起使用,以手动命名 z: 和 c: 来自的命名空间。 I was only doing one or the other, not both combined.我只做一个或另一个,而不是两者结合。 Thank you guys for the help.谢谢你们的帮助。

[XmlAttribute(Namespace = "http://schemas.microsoft.com/2003/10/Serialization/")]
public int Id { get; set; }

[XmlAttribute(Namespace = "http://schemas.microsoft.com/2003/10/Serialization/")]
public int Ref { get; set; }

private bool _nil;

[XmlAttribute("nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public bool Nil
{
    get
    {
        return _nil;
    }
    set
    {
        _nil = value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM