1

I have the following XML:

    <MovieRunTimes>
      <ShowDate>6/9/2012</ShowDate>
      <ShowTimesByDate xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        <a:string>12:25</a:string>
        <a:string>17:30</a:string>
        <a:string>22:35</a:string>
      </ShowTimesByDate>
      <TicketURI>http://www.fandango.com/tms.asp?t=AANCC&amp;m=112244&amp;d=2012-06-09</TicketURI>
    </MovieRunTimes>

And the following C# class:

public class MovieRunTimes
{
    [XmlElement("ShowDate")]
    public string ShowDate { get; set; }

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

    [XmlArray("ShowTimesByDate", Namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays")]
    public List<string> ShowTimesByDate { get; set; }

}

Unfortunately the ShowTimesByDate is empty after I deserialize. If I remove the namespace from the ShowTimesByDate element and the prefix from the string element, then it deserializes fine. How do I correctly use the namespace to deserialize the XML?

2 Answers 2

4

I discovered how to do this. If I amend the class to:

public class MovieRunTimes
{
    [XmlElement("ShowDate")]
    public string ShowDate { get; set; }

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

    [XmlArray("ShowTimesByDate")]
    [XmlArrayItem(Namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays")]
    public List<string> ShowTimesByDate { get; set; }

}

It deserializes correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

dude - pounded my head for six hours on same exact problem. scoured MSDN and SO. The solution is irritating though, because if you have a namespace for the outermost element (MoveRunTimes) then the ns applies to that name and the elements within it...so you would think the array ns applies to the array element itself, not its elements ONLY.
1

The trick is to add a namespace prefix ("a" in your case) to your Collection wrapper element:

<MovieRunTimes >
  <ShowDate>6/9/2012</ShowDate>
  <a:ShowTimesByDate xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <a:string>12:25</a:string>
    <a:string>17:30</a:string>
    <a:string>22:35</a:string>
  </a:ShowTimesByDate>
  <TicketURI>http://www.fandango.com/tms.asp?t=AANCC&amp;m=112244&amp;d=2012-06-09</TicketURI>
</MovieRunTimes>

That is how it comes out after serializing with this code:

        XmlSerializer xs = new XmlSerializer(typeof(MovieRunTimes));
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("a", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
        string result = null;
        using(StringWriter writer = new StringWriter())
        {
            xs.Serialize(writer,mrt,ns);
            result = writer.ToString();
        }

2 Comments

Unfortunately I am consuming the XML, not generating it :(
If that's the case you could either add the missing namespace preffix with XSLT or with LINQ or other XML libraries I guess prior deserializing..

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.