0

I have the problem that I got xml from a REST service and have to deserialize it to an object structure and there is a type="proglang" in it.

<listResult>
   <listEntry xsi:type="proglang">
      <id>0</id>
      <name>C#</name>
   </listEntry>
   <listEntry xsi:type="proglang">
      <id>0</id>
      <name>C#</name>
   </listEntry>
</listResult>

How should the object model with the xml attributes look like in c#?

My Code so far:

[XmlRoot("listResult")]
public class ListResult
{
    [XmlElement("listEntry")]
    //[XmlArrayItem(Type=typeof(proglang))]
    public List<proglang> listEntry;
}

public class proglang
{
    [XmlElement("code")]
    public int id;
    [XmlElement("label")]
    public string name;
}

and the resulting XML from serializing:

<listResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <listEntry>
    <code>1</code>
    <label>C#</label>
  </listEntry>
  <listEntry>
    <code>1</code>
    <label>C++</label>
  </listEntry>
</listResult>
5
  • Can you show us your existing C# code? Commented Jul 11, 2017 at 10:10
  • 1
    So the REST service xml doesn't declare the xsi namespace? Commented Jul 11, 2017 at 11:29
  • no xsi available, but its a class with an id and a name. Commented Jul 11, 2017 at 11:39
  • That XML is syntactically invalid. The namespace with prefix xsi is never declared. Upload to xmlvalidation.com and you will see the error Line 2 Column 35: The prefix "xsi" for attribute "xsi:type" associated with an element type "listEntry" is not bound. Is your really invalid, or did you simply omit the xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance" attribute when typing the XML into the question? Commented Jul 15, 2017 at 3:08
  • If the XML were valid, you could introduce a surrogate psuedo-polymorphic object [] array property along the lines of the single-object surrogate property in this answer. But if the XML is really invalid that won't help at all. Commented Jul 16, 2017 at 20:30

0

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.