Skip to main content
Rollback to Revision 2
Source Link
John McDonald
  • 6.8k
  • 2
  • 32
  • 45

Using XNA's XML content importer, is it possible to read in an array of objects with different subtypes?

For instance, assume these are my class definitions:

public abstract class MyBaseClass
{
    public string MyBaseData;
}

public class MySubClass0 : MyBaseClass
{
    public int MySubData0;
}

public class MySubClass1 : MyBaseClass
{
    public bool MySubData1;
}

And this is my XML file:

<XnaContent>
    <Asset Type="MyBaseClass[]">
        <Item> <!-- I want this to be an instance of MySubClass0 -->
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item>  <!-- I want this to be an instance of MySubClass1 -->
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>

How do I specify that I want the first Item to be an instance of MySubclass0 and the second Item to be an instance of MySubclass1?

Edit: I managed to answer my own question by hand creating an instance of my object array and then following the instructions in this article to write the array to xml: http://msdn.microsoft.com/en-us/library/ff604982.aspx

Looking at the output, it appears I needed a Type attribute on each Item node to specify the type. So my modified XML file looks like this:

<XnaContent>
    <Asset Type="MyBaseClass[]">
        <Item Type="MySubClass0"> 
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item Type="MySubClass1">
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>

Using XNA's XML content importer, is it possible to read in an array of objects with different subtypes?

For instance, assume these are my class definitions:

public abstract class MyBaseClass
{
    public string MyBaseData;
}

public class MySubClass0 : MyBaseClass
{
    public int MySubData0;
}

public class MySubClass1 : MyBaseClass
{
    public bool MySubData1;
}

And this is my XML file:

<XnaContent>
    <Asset Type="MyBaseClass[]">
        <Item> <!-- I want this to be an instance of MySubClass0 -->
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item>  <!-- I want this to be an instance of MySubClass1 -->
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>

How do I specify that I want the first Item to be an instance of MySubclass0 and the second Item to be an instance of MySubclass1?

Edit: I managed to answer my own question by hand creating an instance of my object array and then following the instructions in this article to write the array to xml: http://msdn.microsoft.com/en-us/library/ff604982.aspx

Looking at the output, it appears I needed a Type attribute on each Item node to specify the type. So my modified XML file looks like this:

<XnaContent>
    <Asset Type="MyBaseClass[]">
        <Item Type="MySubClass0"> 
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item Type="MySubClass1">
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>

Using XNA's XML content importer, is it possible to read in an array of objects with different subtypes?

For instance, assume these are my class definitions:

public abstract class MyBaseClass
{
    public string MyBaseData;
}

public class MySubClass0 : MyBaseClass
{
    public int MySubData0;
}

public class MySubClass1 : MyBaseClass
{
    public bool MySubData1;
}

And this is my XML file:

<XnaContent>
    <Asset Type="MyBaseClass[]">
        <Item> <!-- I want this to be an instance of MySubClass0 -->
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item>  <!-- I want this to be an instance of MySubClass1 -->
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>

How do I specify that I want the first Item to be an instance of MySubclass0 and the second Item to be an instance of MySubclass1?

Tweeted twitter.com/#!/StackGameDev/status/186824973577502723
added 784 characters in body
Source Link
Mcguirk
  • 61
  • 1
  • 6

Using XNA's XML content importer, is it possible to read in an array of objects with different subtypes?

For instance, assume these are my class definitions:

public abstract class MyBaseClass
{
    public string MyBaseData;
}

public class MySubClass0 : MyBaseClass
{
    public int MySubData0;
}

public class MySubClass1 : MyBaseClass
{
    public bool MySubData1;
}

And this is my XML file:

<XnaContent>
    <Asset Type="MyBaseClass[]">
        <Item> <!-- I want this to be an instance of MySubClass0 -->
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item>  <!-- I want this to be an instance of MySubClass1 -->
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>

How do I specify that I want the first Item to be an instance of MySubclass0 and the second Item to be an instance of MySubclass1?

Edit: I managed to answer my own question by hand creating an instance of my object array and then following the instructions in this article to write the array to xml: http://msdn.microsoft.com/en-us/library/ff604982.aspx

Looking at the output, it appears I needed a Type attribute on each Item node to specify the type. So my modified XML file looks like this:

<XnaContent>
    <Asset Type="MyBaseClass[]">
        <Item Type="MySubClass0"> 
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item Type="MySubClass1">
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>

Using XNA's XML content importer, is it possible to read in an array of objects with different subtypes?

For instance, assume these are my class definitions:

public abstract class MyBaseClass
{
    public string MyBaseData;
}

public class MySubClass0 : MyBaseClass
{
    public int MySubData0;
}

public class MySubClass1 : MyBaseClass
{
    public bool MySubData1;
}

And this is my XML file:

<XnaContent>
    <Asset Type="MyBaseClass[]">
        <Item> <!-- I want this to be an instance of MySubClass0 -->
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item>  <!-- I want this to be an instance of MySubClass1 -->
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>

How do I specify that I want the first Item to be an instance of MySubclass0 and the second Item to be an instance of MySubclass1?

Using XNA's XML content importer, is it possible to read in an array of objects with different subtypes?

For instance, assume these are my class definitions:

public abstract class MyBaseClass
{
    public string MyBaseData;
}

public class MySubClass0 : MyBaseClass
{
    public int MySubData0;
}

public class MySubClass1 : MyBaseClass
{
    public bool MySubData1;
}

And this is my XML file:

<XnaContent>
    <Asset Type="MyBaseClass[]">
        <Item> <!-- I want this to be an instance of MySubClass0 -->
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item>  <!-- I want this to be an instance of MySubClass1 -->
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>

How do I specify that I want the first Item to be an instance of MySubclass0 and the second Item to be an instance of MySubclass1?

Edit: I managed to answer my own question by hand creating an instance of my object array and then following the instructions in this article to write the array to xml: http://msdn.microsoft.com/en-us/library/ff604982.aspx

Looking at the output, it appears I needed a Type attribute on each Item node to specify the type. So my modified XML file looks like this:

<XnaContent>
    <Asset Type="MyBaseClass[]">
        <Item Type="MySubClass0"> 
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item Type="MySubClass1">
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>
added 2 characters in body
Source Link
Mcguirk
  • 61
  • 1
  • 6

Using XNA's XML content importer, is it possible to read in an array of objects with different subtypes?

For instance, assume these are my class definitions:

public abstract class MyBaseClass
{
    public string MyBaseData;
}

public class MySubClass0 : MyBaseClass
{
    public int MySubData0;
}

public class MySubClass1 : MyBaseClass
{
    public bool MySubData1;
}

And this is my XML file:

<XnaContent>
    <Asset Type="MyBaseClass">Type="MyBaseClass[]">
        <Item> <!-- I want this to be an instance of MySubClass0 -->
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item>  <!-- I want this to be an instance of MySubClass1 -->
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>

How do I specify that I want the first Item to be an instance of MySubclass0 and the second Item to be an instance of MySubclass1?

Using XNA's XML content importer, is it possible to read in an array of objects with different subtypes?

For instance, assume these are my class definitions:

public abstract class MyBaseClass
{
    public string MyBaseData;
}

public class MySubClass0 : MyBaseClass
{
    public int MySubData0;
}

public class MySubClass1 : MyBaseClass
{
    public bool MySubData1;
}

And this is my XML file:

<XnaContent>
    <Asset Type="MyBaseClass">
        <Item> <!-- I want this to be an instance of MySubClass0 -->
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item>  <!-- I want this to be an instance of MySubClass1 -->
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>

How do I specify that I want the first Item to be an instance of MySubclass0 and the second Item to be an instance of MySubclass1?

Using XNA's XML content importer, is it possible to read in an array of objects with different subtypes?

For instance, assume these are my class definitions:

public abstract class MyBaseClass
{
    public string MyBaseData;
}

public class MySubClass0 : MyBaseClass
{
    public int MySubData0;
}

public class MySubClass1 : MyBaseClass
{
    public bool MySubData1;
}

And this is my XML file:

<XnaContent>
    <Asset Type="MyBaseClass[]">
        <Item> <!-- I want this to be an instance of MySubClass0 -->
            <MyBaseData>alpha</MyBaseData>
            <MySubData0>314</MySubData0>
        </Item>
        <Item>  <!-- I want this to be an instance of MySubClass1 -->
            <MyBaseData>bravo</MyBaseData>
            <MySubData1>true</MySubData1>
        </Item>
    </Asset>
</XnaContent>

How do I specify that I want the first Item to be an instance of MySubclass0 and the second Item to be an instance of MySubclass1?

Source Link
Mcguirk
  • 61
  • 1
  • 6
Loading