1

Data ("1.0.0") from attribute "version" fall into property. But the property "Event" in the same class remains empty. If cut namespace from XML, it will work.

My XML:

<rootnode>
  <ns:eventresponse xmlns:ns="somenamespace" version="1.0.0">
    <event id="694717028">
      <somedata>val</somedata>
    </event>
  </ns:eventresponse>
</rootnode>

My class:

[XmlRoot("rootnode")]
public class RootNode
{
    [XmlElement(ElementName = "eventresponse", Namespace = "somenamespace")]
    public EventResponseData EventResponse { get; set; }
}

  public class EventResponseData
    {
        [XmlElement("event")]
        public EventData Event { get; set; }

        [XmlAttribute("version")]
        public string Version { get; set; }
    }

public class TvEventData
{
    [XmlAttribute("id")]
    public string EventID { get; set; } 

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

My Deserializer:

using (var reader = XmlReader.Create(new StringReader(xml)))
{
     reader.MoveToContent();
     var obj = (T)new XmlSerializer(typeof(T)).Deserialize(reader);
} 
1
  • Please, do not include a languge tag in a title unless it wouldn't made sense without it. Tags serve this purpose. Commented Feb 21, 2014 at 10:33

1 Answer 1

4

Your event element is implicitly in the same namespace in the XML due to namespace defaulting, so you should specify that in your declaration:

public class EventResponseData
{
    [XmlElement(ElementName = "event", Namespace = "somenamespace")]
    public EventData Event { get; set; }

    [XmlAttribute("version")]
    public string Version { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.