Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Xml seriliazation query

Xml seriliazation query

Scheduled Pinned Locked Moved C#
helpdatabasexmlquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    avneeshb
    wrote on last edited by
    #1

    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!

    H 1 Reply Last reply
    0
    • A avneeshb

      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!

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      I see no subclasses. SubTest does not extend Test, i.e. you must code it as SubText : Test.

      Microsoft MVP, Visual C# My Articles

      A 1 Reply Last reply
      0
      • H Heath Stewart

        I see no subclasses. SubTest does not extend Test, i.e. you must code it as SubText : Test.

        Microsoft MVP, Visual C# My Articles

        A Offline
        A Offline
        avneeshb
        wrote on last edited by
        #3

        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!

        H 1 Reply Last reply
        0
        • A avneeshb

          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!

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          When you create your XmlSerializer instance, make sure you create it using either your instance of SubTest or the type of SubTest (using typeof(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

          A 1 Reply Last reply
          0
          • H Heath Stewart

            When you create your XmlSerializer instance, make sure you create it using either your instance of SubTest or the type of SubTest (using typeof(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

            A Offline
            A Offline
            avneeshb
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups