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. Element name of serialized List<t></t>

Element name of serialized List<t></t>

Scheduled Pinned Locked Moved C#
jsonhelpquestion
7 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.
  • N Offline
    N Offline
    Nathan Gloyn
    wrote on last edited by
    #1

    I'm starting to pull my hair out and hope somebody can help stop me from becoming bald! I have a Class - LookupItem that uses XmlRoot attribute so that when serialized individually outputs under element named Item. Now if I have a List when I serialize it the lookup item class always serializes as LookupItem and not Item. What I'm wanting to see is: <Clean> <Item> .... </Item> </Clean> but all I get is: <Clean> <LookupItem> .... </LookupItem> </Clean> The code I'm currently using to do the serialization is below, but I have tried doing serialization with extra types, XmlAttributeOverrides and still cannot get the output correct. XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlAttributeOverrides xao = new XmlAttributeOverrides(); XmlRootAttribute xr = new XmlRootAttribute("Clean"); XmlSerializer s = new XmlSerializer(typeof(LookupItemList)); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; StringWriter output = new StringWriter(); XmlWriter writer = XmlWriter.Create(output, settings); //TextWriter w = new StreamWriter(); s.Serialize(writer, Clean, ns); writer.Close(); output.ToString(); Can anybody help me at all?

    T 1 Reply Last reply
    0
    • N Nathan Gloyn

      I'm starting to pull my hair out and hope somebody can help stop me from becoming bald! I have a Class - LookupItem that uses XmlRoot attribute so that when serialized individually outputs under element named Item. Now if I have a List when I serialize it the lookup item class always serializes as LookupItem and not Item. What I'm wanting to see is: <Clean> <Item> .... </Item> </Clean> but all I get is: <Clean> <LookupItem> .... </LookupItem> </Clean> The code I'm currently using to do the serialization is below, but I have tried doing serialization with extra types, XmlAttributeOverrides and still cannot get the output correct. XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlAttributeOverrides xao = new XmlAttributeOverrides(); XmlRootAttribute xr = new XmlRootAttribute("Clean"); XmlSerializer s = new XmlSerializer(typeof(LookupItemList)); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; StringWriter output = new StringWriter(); XmlWriter writer = XmlWriter.Create(output, settings); //TextWriter w = new StreamWriter(); s.Serialize(writer, Clean, ns); writer.Close(); output.ToString(); Can anybody help me at all?

      T Offline
      T Offline
      TJoe
      wrote on last edited by
      #2

      I think you need to apply the XmlRootAttribute to the LookupItemList class. So Something like:

      [XmlRoot("Clean")]
      public class LookupItemList ...

      Then you would need to do the same thing with the LookupItem class, but use XmlElementAttribute to define the name as "Item". FYI...In your code example above you don't use xr or xao anywhere, are you sure this is the code you are using?

      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

      N 1 Reply Last reply
      0
      • T TJoe

        I think you need to apply the XmlRootAttribute to the LookupItemList class. So Something like:

        [XmlRoot("Clean")]
        public class LookupItemList ...

        Then you would need to do the same thing with the LookupItem class, but use XmlElementAttribute to define the name as "Item". FYI...In your code example above you don't use xr or xao anywhere, are you sure this is the code you are using?

        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

        N Offline
        N Offline
        Nathan Gloyn
        wrote on last edited by
        #3

        Sorry Tom, Just realised code posted not quite correct I'm using: XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer s = new XmlSerializer(typeof(LookupItem)); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; StringWriter output = new StringWriter(); XmlWriter writer = XmlWriter.Create(output, settings); //TextWriter w = new StreamWriter(); s.Serialize(writer, toSerialize, ns); writer.Close(); return output.ToString(); The root element won't help unfortunately since I'm trying to serialise a List, even if I did create a specialist list based on type T all I get output is the correct root element, the issue is the items in the list being output with the correct element name as specified by myself using XmlRoot("Item") on the class that is stored in the list.

        T 1 Reply Last reply
        0
        • N Nathan Gloyn

          Sorry Tom, Just realised code posted not quite correct I'm using: XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer s = new XmlSerializer(typeof(LookupItem)); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; StringWriter output = new StringWriter(); XmlWriter writer = XmlWriter.Create(output, settings); //TextWriter w = new StreamWriter(); s.Serialize(writer, toSerialize, ns); writer.Close(); return output.ToString(); The root element won't help unfortunately since I'm trying to serialise a List, even if I did create a specialist list based on type T all I get output is the correct root element, the issue is the items in the list being output with the correct element name as specified by myself using XmlRoot("Item") on the class that is stored in the list.

          T Offline
          T Offline
          TJoe
          wrote on last edited by
          #4

          For the LookupItem class you should use the XmlElementAttribute (I think). So something like this:

          [XmlElement("Item")]
          public class LookupItem ...

          The XmlRootAttribute is only good for the root item in the XML doc (of which there is only one). Let me know if this doesn't work, though.

          Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

          N 1 Reply Last reply
          0
          • T TJoe

            For the LookupItem class you should use the XmlElementAttribute (I think). So something like this:

            [XmlElement("Item")]
            public class LookupItem ...

            The XmlRootAttribute is only good for the root item in the XML doc (of which there is only one). Let me know if this doesn't work, though.

            Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

            N Offline
            N Offline
            Nathan Gloyn
            wrote on last edited by
            #5

            XmlElement can not be used on a class only property, index, field or param. The XmlRoot attribute simply tells the serializer that for this particular class to use a specific name for the node, it doesn't directly relate to the root element of the doc. Cheers Nathan

            T 1 Reply Last reply
            0
            • N Nathan Gloyn

              XmlElement can not be used on a class only property, index, field or param. The XmlRoot attribute simply tells the serializer that for this particular class to use a specific name for the node, it doesn't directly relate to the root element of the doc. Cheers Nathan

              T Offline
              T Offline
              TJoe
              wrote on last edited by
              #6

              Sorry, my memory isn't what is used to be ;-) Here is the code that should do what you want...

              [XmlType(TypeName = "Item")]
              public class LookupItem {
              // TODO
              }

              [XmlRoot("Clean")]
              public class LookupItemList : List<LookupItem> {
              // TODO
              }

              I'm assuming you are using a generic List and that you are serializing the List as the root object.

              Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

              N 1 Reply Last reply
              0
              • T TJoe

                Sorry, my memory isn't what is used to be ;-) Here is the code that should do what you want...

                [XmlType(TypeName = "Item")]
                public class LookupItem {
                // TODO
                }

                [XmlRoot("Clean")]
                public class LookupItemList : List<LookupItem> {
                // TODO
                }

                I'm assuming you are using a generic List and that you are serializing the List as the root object.

                Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                N Offline
                N Offline
                Nathan Gloyn
                wrote on last edited by
                #7

                XmlType - of course! Got so hung up on the XmlRoot that I completely forgot about that. Got it all serializing lovely now, managed it without a specific class by simply providing a XmlRoot attribute to the serializer. Thanks Tom

                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