Xml seriliazation query
-
Hi! I am trying to use XmlSerialization in the following scenario: 1. I have a class called Test: public class Test { private int someAttribute; [XmlAttribute] public int Att { get { return someAttribute; } set { someAttribute=value; } } } 2. And a subclass: public class SubTest { public string something; } Upon serilialization, I get the following: string As can be seen the attribute Att is not visible in the subclass. Using virtual (for the property) and overriding it, or just hiding it with 'new' also did not help. Creating a new attribute with the same name (i.e. marking it as [XmlAttribute("Att")] also creates an error. Any suggestion as to how I can have an attribute of the same name in the subclass also? Thanks!
-
Hi! I am trying to use XmlSerialization in the following scenario: 1. I have a class called Test: public class Test { private int someAttribute; [XmlAttribute] public int Att { get { return someAttribute; } set { someAttribute=value; } } } 2. And a subclass: public class SubTest { public string something; } Upon serilialization, I get the following: string As can be seen the attribute Att is not visible in the subclass. Using virtual (for the property) and overriding it, or just hiding it with 'new' also did not help. Creating a new attribute with the same name (i.e. marking it as [XmlAttribute("Att")] also creates an error. Any suggestion as to how I can have an attribute of the same name in the subclass also? Thanks!
I see no subclasses.
SubTest
does not extendTest
, i.e. you must code it asSubText : Test
.Microsoft MVP, Visual C# My Articles
-
I see no subclasses.
SubTest
does not extendTest
, i.e. you must code it asSubText : Test
.Microsoft MVP, Visual C# My Articles
-
thats what happens when you don't copy and paste :) In my actual code Subtest extends test so: public class SubTest:Test { public string something; } Thanks!
When you create your
XmlSerializer
instance, make sure you create it using either your instance ofSubTest
or the type ofSubTest
(usingtypeof(SubTest)
). The following sample works just fine:using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;public class Test
{
static void Main()
{
SubTest test = new SubTest();XmlTextWriter writer = new XmlTextWriter("Test.xml", Encoding.UTF8); XmlSerializer serializer = new XmlSerializer(typeof(SubTest)); serializer.Serialize(writer, test);
}
[XmlAttribute]
public int Attr
{
get { return 1; }
set { ; }
}
}public class SubTest : Test
{
public string Something = "Test";
}Microsoft MVP, Visual C# My Articles
-
When you create your
XmlSerializer
instance, make sure you create it using either your instance ofSubTest
or the type ofSubTest
(usingtypeof(SubTest)
). The following sample works just fine:using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;public class Test
{
static void Main()
{
SubTest test = new SubTest();XmlTextWriter writer = new XmlTextWriter("Test.xml", Encoding.UTF8); XmlSerializer serializer = new XmlSerializer(typeof(SubTest)); serializer.Serialize(writer, test);
}
[XmlAttribute]
public int Attr
{
get { return 1; }
set { ; }
}
}public class SubTest : Test
{
public string Something = "Test";
}Microsoft MVP, Visual C# My Articles
Thanks! I was actually trying to do this as part of creating a webservice, and apologies for not making this more specific. I have a webmethod which looks something like: MyWebMethod(SubTest sub){ somestuff; } When I create the webservice and look at the soap message that would be sent, I don't see the attribute there (i.e. the string looks like string instead of string . I guess the default xmlserializer does not pick the derived attribute. Is there anyway of informing the serializer in the context of writing a webservice? Thanks!