Serialization with XmlSerializer: how to set the XML root node to something different from <ArrayOfClassname>????

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Rock

    Serialization with XmlSerializer: how to set the XML root node to something different from <ArrayOfClassname>????

    Hello,

    when serializing an array of elements of a class Classname using
    XmlSerializer.S erialize() I get an XML like the following:

    <?xml version="1.0">
    <ArrayOfClassna me>
    .......
    .......
    </ArrayOfClassnam e>

    I'd like to be able to set the XML root node to something different from
    <ArrayOfClassna me> .... for example something like <Classnames>.
    As an alternative when deserilizing an XML such as the following:

    <?xml version="1.0">
    <Classnames>
    .......
    .......
    </Classnames>

    I'd like to be able to "load" it into an array of objects of class Classname
    (at the moment when making such an attempt I get an obvious exception
    stating "<Classname s> was not expected").


    Bob Rock


  • Dino Chiesa [Microsoft]

    #2
    Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

    XmlArrayAttribu te

    see reference:
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


    and guide:
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


    -Dino
    ps: no need to cross-post


    "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
    news:O3JZzQ7UEH A.808@tk2msftng p13.phx.gbl...[color=blue]
    > Hello,
    >
    > when serializing an array of elements of a class Classname using
    > XmlSerializer.S erialize() I get an XML like the following:
    >
    > <?xml version="1.0">
    > <ArrayOfClassna me>
    > ......
    > ......
    > </ArrayOfClassnam e>
    >
    > I'd like to be able to set the XML root node to something different from
    > <ArrayOfClassna me> .... for example something like <Classnames>.
    > As an alternative when deserilizing an XML such as the following:
    >
    > <?xml version="1.0">
    > <Classnames>
    > ......
    > ......
    > </Classnames>
    >
    > I'd like to be able to "load" it into an array of objects of class[/color]
    Classname[color=blue]
    > (at the moment when making such an attempt I get an obvious exception
    > stating "<Classname s> was not expected").
    >
    >
    > Bob Rock
    >
    >[/color]


    Comment

    • Dino Chiesa [Microsoft]

      #3
      Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

      XmlArrayAttribu te

      see reference:
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


      and guide:
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


      -Dino
      ps: no need to cross-post


      "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
      news:O3JZzQ7UEH A.808@tk2msftng p13.phx.gbl...[color=blue]
      > Hello,
      >
      > when serializing an array of elements of a class Classname using
      > XmlSerializer.S erialize() I get an XML like the following:
      >
      > <?xml version="1.0">
      > <ArrayOfClassna me>
      > ......
      > ......
      > </ArrayOfClassnam e>
      >
      > I'd like to be able to set the XML root node to something different from
      > <ArrayOfClassna me> .... for example something like <Classnames>.
      > As an alternative when deserilizing an XML such as the following:
      >
      > <?xml version="1.0">
      > <Classnames>
      > ......
      > ......
      > </Classnames>
      >
      > I'd like to be able to "load" it into an array of objects of class[/color]
      Classname[color=blue]
      > (at the moment when making such an attempt I get an obvious exception
      > stating "<Classname s> was not expected").
      >
      >
      > Bob Rock
      >
      >[/color]


      Comment

      • Bob Rock

        #4
        Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

        "Dino Chiesa [Microsoft]" <dinoch@online. microsoft.com> wrote in message
        news:uzEM9Z7UEH A.2320@TK2MSFTN GP10.phx.gbl...[color=blue]
        > XmlArrayAttribu te
        >
        > see reference:
        >[/color]
        http://msdn.microsoft.com/library/en...ClassTopic.asp[color=blue]
        >
        > and guide:
        >[/color]
        http://msdn.microsoft.com/library/en...attributes.asp[color=blue]
        >
        > -Dino
        > ps: no need to cross-post
        >
        >[/color]

        Dino,

        I'm using Xmlserializer not implicitly within a web method but explicitly
        within my code. So the question is, how do I apply the XmlArrayAttribu te????
        From the documentation is seems that this *should* be possible, but how???

        Sorry for the cross-posting, but it is done only in the hope that more
        people may read (and hopefully answer) my post.

        Grazie mille.

        Ciao,
        Bob Rock


        Comment

        • Bob Rock

          #5
          Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

          "Dino Chiesa [Microsoft]" <dinoch@online. microsoft.com> wrote in message
          news:uzEM9Z7UEH A.2320@TK2MSFTN GP10.phx.gbl...[color=blue]
          > XmlArrayAttribu te
          >
          > see reference:
          >[/color]
          http://msdn.microsoft.com/library/en...ClassTopic.asp[color=blue]
          >
          > and guide:
          >[/color]
          http://msdn.microsoft.com/library/en...attributes.asp[color=blue]
          >
          > -Dino
          > ps: no need to cross-post
          >
          >[/color]

          Dino,

          I'm using Xmlserializer not implicitly within a web method but explicitly
          within my code. So the question is, how do I apply the XmlArrayAttribu te????
          From the documentation is seems that this *should* be possible, but how???

          Sorry for the cross-posting, but it is done only in the hope that more
          people may read (and hopefully answer) my post.

          Grazie mille.

          Ciao,
          Bob Rock


          Comment

          • Mickey Williams

            #6
            Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

            Maybe not exactly what you want, but you can get close like this:

            public class Foo
            {
            ArrayList _classNames = new ArrayList();
            [XmlArray("Class Names"), XmlArrayItem("C lassName")]
            public string[] ClassNames
            {
            get { return (string[])_classNames.To Array(typeof(st ring)); }
            set {_classNames = new ArrayList(value );}
            }
            }

            Construct and serialize a Foo like this:

            string[] names = new string[]{"f", "g", "h"};
            Foo f = new Foo();
            f.ClassNames = names;
            Console.WriteLi ne(SerializeThi ngToXmlString(f ));


            Where the helper method is:

            static string SerializeThingT oXmlString(obje ct thing)
            {
            StringWriter stringWriter = new StringWriter();
            XmlSerializer serializer = new XmlSerializer(t hing.GetType()) ;
            serializer.Seri alize(stringWri ter, thing);
            return stringWriter.To String();
            }

            --
            Mickey Williams
            Author, "Microsoft Visual C# .NET Core Reference", MS Press



            Comment

            • Mickey Williams

              #7
              Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

              Maybe not exactly what you want, but you can get close like this:

              public class Foo
              {
              ArrayList _classNames = new ArrayList();
              [XmlArray("Class Names"), XmlArrayItem("C lassName")]
              public string[] ClassNames
              {
              get { return (string[])_classNames.To Array(typeof(st ring)); }
              set {_classNames = new ArrayList(value );}
              }
              }

              Construct and serialize a Foo like this:

              string[] names = new string[]{"f", "g", "h"};
              Foo f = new Foo();
              f.ClassNames = names;
              Console.WriteLi ne(SerializeThi ngToXmlString(f ));


              Where the helper method is:

              static string SerializeThingT oXmlString(obje ct thing)
              {
              StringWriter stringWriter = new StringWriter();
              XmlSerializer serializer = new XmlSerializer(t hing.GetType()) ;
              serializer.Seri alize(stringWri ter, thing);
              return stringWriter.To String();
              }

              --
              Mickey Williams
              Author, "Microsoft Visual C# .NET Core Reference", MS Press



              Comment

              • Bob Rock

                #8
                Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

                > Maybe not exactly what you want, but you can get close like this:[color=blue]
                >
                > public class Foo
                > {
                > ArrayList _classNames = new ArrayList();
                > [XmlArray("Class Names"), XmlArrayItem("C lassName")]
                > public string[] ClassNames
                > {
                > get { return (string[])_classNames.To Array(typeof(st ring)); }
                > set {_classNames = new ArrayList(value );}
                > }
                > }
                >
                > Construct and serialize a Foo like this:
                >
                > string[] names = new string[]{"f", "g", "h"};
                > Foo f = new Foo();
                > f.ClassNames = names;
                > Console.WriteLi ne(SerializeThi ngToXmlString(f ));
                >
                >
                > Where the helper method is:
                >
                > static string SerializeThingT oXmlString(obje ct thing)
                > {
                > StringWriter stringWriter = new StringWriter();
                > XmlSerializer serializer = new XmlSerializer(t hing.GetType()) ;
                > serializer.Seri alize(stringWri ter, thing);
                > return stringWriter.To String();
                > }
                >
                > --
                > Mickey Williams
                > Author, "Microsoft Visual C# .NET Core Reference", MS Press
                > www.servergeek.com/blogs/mickey
                >[/color]

                Ahhh, so that is the way you can use the XmlArrayAttribu te and
                XmlArrayItemAtt ribute on something that is not a field!!!
                Is it possible to use it on methods returning arrays???

                I've also seen that there is a way of setting the root node element to
                whatever one wants using the XmlSerializer(T ype, XmlRootAttribut e)
                constructor. Unfortunately when deserializing I need to change the root node
                element to <ArrayOfClassna me> as the XmlSerializer expects to avoid the
                exception.


                Bob Rock






                Comment

                • Bob Rock

                  #9
                  Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

                  > Maybe not exactly what you want, but you can get close like this:[color=blue]
                  >
                  > public class Foo
                  > {
                  > ArrayList _classNames = new ArrayList();
                  > [XmlArray("Class Names"), XmlArrayItem("C lassName")]
                  > public string[] ClassNames
                  > {
                  > get { return (string[])_classNames.To Array(typeof(st ring)); }
                  > set {_classNames = new ArrayList(value );}
                  > }
                  > }
                  >
                  > Construct and serialize a Foo like this:
                  >
                  > string[] names = new string[]{"f", "g", "h"};
                  > Foo f = new Foo();
                  > f.ClassNames = names;
                  > Console.WriteLi ne(SerializeThi ngToXmlString(f ));
                  >
                  >
                  > Where the helper method is:
                  >
                  > static string SerializeThingT oXmlString(obje ct thing)
                  > {
                  > StringWriter stringWriter = new StringWriter();
                  > XmlSerializer serializer = new XmlSerializer(t hing.GetType()) ;
                  > serializer.Seri alize(stringWri ter, thing);
                  > return stringWriter.To String();
                  > }
                  >
                  > --
                  > Mickey Williams
                  > Author, "Microsoft Visual C# .NET Core Reference", MS Press
                  > www.servergeek.com/blogs/mickey
                  >[/color]

                  Ahhh, so that is the way you can use the XmlArrayAttribu te and
                  XmlArrayItemAtt ribute on something that is not a field!!!
                  Is it possible to use it on methods returning arrays???

                  I've also seen that there is a way of setting the root node element to
                  whatever one wants using the XmlSerializer(T ype, XmlRootAttribut e)
                  constructor. Unfortunately when deserializing I need to change the root node
                  element to <ArrayOfClassna me> as the XmlSerializer expects to avoid the
                  exception.


                  Bob Rock






                  Comment

                  • Dino Chiesa [Microsoft]

                    #10
                    Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

                    > Ahhh, so that is the way you can use the XmlArrayAttribu te and[color=blue]
                    > XmlArrayItemAtt ribute on something that is not a field!!!
                    > Is it possible to use it on methods returning arrays???[/color]

                    Methods can return instances of Foo. Those instances of Foo will behave as
                    indicated by the attributes on Foo.
                    If your method returns an array, then the attributes that apply are those on
                    the base type.
                    So if it is an array of Foo that is being returned, then the attributes on
                    the Foo definition will apply to the elements of the array.

                    I believe it is not possible to effectively attribute arrays if they are not
                    serialized as members of classes. In other words, if it is a local variable
                    and you want to serialize it, I believe the attributes do not apply. Also
                    if the array is a member variable and you do not serialize the entire class,
                    then I believe the attributes do not apply.
                    [color=blue]
                    > I've also seen that there is a way of setting the root node element to
                    > whatever one wants using the XmlSerializer(T ype, XmlRootAttribut e)
                    > constructor. Unfortunately when deserializing I need to change the root[/color]
                    node[color=blue]
                    > element to <ArrayOfClassna me> as the XmlSerializer expects to avoid the
                    > exception.[/color]

                    Yes this should work, but I don't understand what the problem is on
                    de-serializing. If you are using the same kind of XmlSerializer
                    (constructed with the XmlRootAttribut e), then the string or stream with the
                    modified root node should de-serialize just fine. eg

                    XmlRootAttribut e xRoot = new XmlRootAttribut e();
                    xRoot.Namespace = "urn:www.exampl e.org";
                    xRoot.ElementNa me = "Bunch";

                    s= new XmlSerializer(t ypeof(Fred[]), xRoot);
                    Fred[] f= {.....};

                    // serialize to a string:
                    StringWriter sw = new StringWriter();
                    s.Serialize( sw, f );
                    string serializedXml = sw.ToString(); // will give you a modified
                    root element name

                    // now de-serialize
                    StringReader sr= new StringReader(se rializedXml);
                    Fred[] f2= (Fred[]) s.Deserialize(n ew System.Xml.XmlT extReader(sr));

                    [color=blue]
                    >Sorry for the cross-posting, but it is done only in the hope that more[/color]
                    people may read (and hopefully answer) my post.

                    Uh huh. I hate to be a stick in the mud about the cross posting, but 1st, I
                    can't help it, it's my nature; and 2nd, this seems like a really really
                    basic case. You are asking specifically about xml serialization, and there
                    is a specific group for xml serialization. A perfect match! Posting more
                    widely doesn't make sense to me. Yes, more people see it. But not more
                    people who care, or who will have anything to contribute. You could also
                    ask xml serialization questions to people you meet in the subway on the way
                    to work, or at church, or in the stands at the ballgame, but I doubt you'll
                    have much success there either.

                    Imagine if everyone followed your policy. Every question on every topic
                    would get asked on every group. Would that work? The different groups are
                    set up to raise the signal-to-noise ratio. If only works if the
                    participants follow the conventions.

                    Sometimes cross-posts make sense because it is not clear what area the
                    question would fall under. 2 groups is reasonable. 3 is about the limit.
                    4 is egregious.

                    I am not making any of this up. This is just standard netiquette.



                    -D


                    "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
                    news:%23jNQDo8U EHA.3420@TK2MSF TNGP12.phx.gbl. ..[color=blue][color=green]
                    > > Maybe not exactly what you want, but you can get close like this:
                    > >
                    > > public class Foo
                    > > {
                    > > ArrayList _classNames = new ArrayList();
                    > > [XmlArray("Class Names"), XmlArrayItem("C lassName")]
                    > > public string[] ClassNames
                    > > {
                    > > get { return (string[])_classNames.To Array(typeof(st ring)); }
                    > > set {_classNames = new ArrayList(value );}
                    > > }
                    > > }
                    > >
                    > > Construct and serialize a Foo like this:
                    > >
                    > > string[] names = new string[]{"f", "g", "h"};
                    > > Foo f = new Foo();
                    > > f.ClassNames = names;
                    > > Console.WriteLi ne(SerializeThi ngToXmlString(f ));
                    > >
                    > >
                    > > Where the helper method is:
                    > >
                    > > static string SerializeThingT oXmlString(obje ct thing)
                    > > {
                    > > StringWriter stringWriter = new StringWriter();
                    > > XmlSerializer serializer = new XmlSerializer(t hing.GetType()) ;
                    > > serializer.Seri alize(stringWri ter, thing);
                    > > return stringWriter.To String();
                    > > }
                    > >
                    > > --
                    > > Mickey Williams
                    > > Author, "Microsoft Visual C# .NET Core Reference", MS Press
                    > > www.servergeek.com/blogs/mickey
                    > >[/color]
                    >[/color]


                    Comment

                    • Dino Chiesa [Microsoft]

                      #11
                      Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

                      > Ahhh, so that is the way you can use the XmlArrayAttribu te and[color=blue]
                      > XmlArrayItemAtt ribute on something that is not a field!!!
                      > Is it possible to use it on methods returning arrays???[/color]

                      Methods can return instances of Foo. Those instances of Foo will behave as
                      indicated by the attributes on Foo.
                      If your method returns an array, then the attributes that apply are those on
                      the base type.
                      So if it is an array of Foo that is being returned, then the attributes on
                      the Foo definition will apply to the elements of the array.

                      I believe it is not possible to effectively attribute arrays if they are not
                      serialized as members of classes. In other words, if it is a local variable
                      and you want to serialize it, I believe the attributes do not apply. Also
                      if the array is a member variable and you do not serialize the entire class,
                      then I believe the attributes do not apply.
                      [color=blue]
                      > I've also seen that there is a way of setting the root node element to
                      > whatever one wants using the XmlSerializer(T ype, XmlRootAttribut e)
                      > constructor. Unfortunately when deserializing I need to change the root[/color]
                      node[color=blue]
                      > element to <ArrayOfClassna me> as the XmlSerializer expects to avoid the
                      > exception.[/color]

                      Yes this should work, but I don't understand what the problem is on
                      de-serializing. If you are using the same kind of XmlSerializer
                      (constructed with the XmlRootAttribut e), then the string or stream with the
                      modified root node should de-serialize just fine. eg

                      XmlRootAttribut e xRoot = new XmlRootAttribut e();
                      xRoot.Namespace = "urn:www.exampl e.org";
                      xRoot.ElementNa me = "Bunch";

                      s= new XmlSerializer(t ypeof(Fred[]), xRoot);
                      Fred[] f= {.....};

                      // serialize to a string:
                      StringWriter sw = new StringWriter();
                      s.Serialize( sw, f );
                      string serializedXml = sw.ToString(); // will give you a modified
                      root element name

                      // now de-serialize
                      StringReader sr= new StringReader(se rializedXml);
                      Fred[] f2= (Fred[]) s.Deserialize(n ew System.Xml.XmlT extReader(sr));

                      [color=blue]
                      >Sorry for the cross-posting, but it is done only in the hope that more[/color]
                      people may read (and hopefully answer) my post.

                      Uh huh. I hate to be a stick in the mud about the cross posting, but 1st, I
                      can't help it, it's my nature; and 2nd, this seems like a really really
                      basic case. You are asking specifically about xml serialization, and there
                      is a specific group for xml serialization. A perfect match! Posting more
                      widely doesn't make sense to me. Yes, more people see it. But not more
                      people who care, or who will have anything to contribute. You could also
                      ask xml serialization questions to people you meet in the subway on the way
                      to work, or at church, or in the stands at the ballgame, but I doubt you'll
                      have much success there either.

                      Imagine if everyone followed your policy. Every question on every topic
                      would get asked on every group. Would that work? The different groups are
                      set up to raise the signal-to-noise ratio. If only works if the
                      participants follow the conventions.

                      Sometimes cross-posts make sense because it is not clear what area the
                      question would fall under. 2 groups is reasonable. 3 is about the limit.
                      4 is egregious.

                      I am not making any of this up. This is just standard netiquette.



                      -D


                      "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
                      news:%23jNQDo8U EHA.3420@TK2MSF TNGP12.phx.gbl. ..[color=blue][color=green]
                      > > Maybe not exactly what you want, but you can get close like this:
                      > >
                      > > public class Foo
                      > > {
                      > > ArrayList _classNames = new ArrayList();
                      > > [XmlArray("Class Names"), XmlArrayItem("C lassName")]
                      > > public string[] ClassNames
                      > > {
                      > > get { return (string[])_classNames.To Array(typeof(st ring)); }
                      > > set {_classNames = new ArrayList(value );}
                      > > }
                      > > }
                      > >
                      > > Construct and serialize a Foo like this:
                      > >
                      > > string[] names = new string[]{"f", "g", "h"};
                      > > Foo f = new Foo();
                      > > f.ClassNames = names;
                      > > Console.WriteLi ne(SerializeThi ngToXmlString(f ));
                      > >
                      > >
                      > > Where the helper method is:
                      > >
                      > > static string SerializeThingT oXmlString(obje ct thing)
                      > > {
                      > > StringWriter stringWriter = new StringWriter();
                      > > XmlSerializer serializer = new XmlSerializer(t hing.GetType()) ;
                      > > serializer.Seri alize(stringWri ter, thing);
                      > > return stringWriter.To String();
                      > > }
                      > >
                      > > --
                      > > Mickey Williams
                      > > Author, "Microsoft Visual C# .NET Core Reference", MS Press
                      > > www.servergeek.com/blogs/mickey
                      > >[/color]
                      >[/color]


                      Comment

                      • Bob Rock

                        #12
                        Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

                        > > Ahhh, so that is the way you can use the XmlArrayAttribu te and[color=blue][color=green]
                        > > XmlArrayItemAtt ribute on something that is not a field!!!
                        > > Is it possible to use it on methods returning arrays???[/color]
                        >
                        > Methods can return instances of Foo. Those instances of Foo will behave[/color]
                        as[color=blue]
                        > indicated by the attributes on Foo.
                        > If your method returns an array, then the attributes that apply are those[/color]
                        on[color=blue]
                        > the base type.
                        > So if it is an array of Foo that is being returned, then the attributes on
                        > the Foo definition will apply to the elements of the array.[/color]

                        The point here is not controlling the serialization of the elements of an
                        array, but of the array itself, in other words of the xml root element.
                        [color=blue]
                        > I believe it is not possible to effectively attribute arrays if they are[/color]
                        not[color=blue]
                        > serialized as members of classes. In other words, if it is a local[/color]
                        variable[color=blue]
                        > and you want to serialize it, I believe the attributes do not apply.[/color]

                        That is not true ... controlling serialization of a class field that is an
                        array is indeed very simple (XmlArrayAttrib ute and XmlArrayItemAtt ribute is
                        all that is needed).
                        [color=blue]
                        > Also
                        > if the array is a member variable and you do not serialize the entire[/color]
                        class,[color=blue]
                        > then I believe the attributes do not apply.
                        >[color=green]
                        > > I've also seen that there is a way of setting the root node element to
                        > > whatever one wants using the XmlSerializer(T ype, XmlRootAttribut e)
                        > > constructor. Unfortunately when deserializing I need to change the root[/color]
                        > node[color=green]
                        > > element to <ArrayOfClassna me> as the XmlSerializer expects to avoid the
                        > > exception.[/color]
                        >
                        > Yes this should work, but I don't understand what the problem is on
                        > de-serializing. If you are using the same kind of XmlSerializer
                        > (constructed with the XmlRootAttribut e), then the string or stream with[/color]
                        the[color=blue]
                        > modified root node should de-serialize just fine. eg[/color]

                        Again, this is not true. Using the XmlSerializer constructor with the
                        XmlRootAttribut e parameter lets you control only serialization ... it does
                        not make the XmlSerializer instance *understand* an xml with the root node
                        set to the XmlRootAttribut e parameter you are passing in when using that
                        XmlSerializer instance to deserialize an xml into the array. It *still*
                        expects the xml that it would generate normally (the one with
                        <ArrayOfClassna me>).
                        [color=blue]
                        >[color=green]
                        > >Sorry for the cross-posting, but it is done only in the hope that more[/color]
                        > people may read (and hopefully answer) my post.
                        >[/color]
                        [color=blue]
                        >. Yes, more people see it. But not more
                        > people who care, or who will have anything to contribute.[/color]

                        This is just your personal opinion.
                        I posted not on *any* NG, but on NGs that I felt are visited by people that
                        could have the answer I was looking for .... xml or web services newsgroups
                        (and I believe serialization is something that should be well known by
                        people dealing with WS).
                        [color=blue]
                        > You could also
                        > ask xml serialization questions to people you meet in the subway on the[/color]
                        way[color=blue]
                        > to work, or at church, or in the stands at the ballgame, but I doubt[/color]
                        you'll[color=blue]
                        > have much success there either.
                        >
                        > Imagine if everyone followed your policy. Every question on every topic
                        > would get asked on every group. Would that work? The different groups[/color]
                        are[color=blue]
                        > set up to raise the signal-to-noise ratio. If only works if the
                        > participants follow the conventions.
                        >
                        > Sometimes cross-posts make sense because it is not clear what area the
                        > question would fall under. 2 groups is reasonable. 3 is about the limit.
                        > 4 is egregious.
                        >
                        > I am not making any of this up. This is just standard netiquette.
                        > http://www.google.com/search?hl=en&n...+cross+posting
                        >
                        >
                        > -D
                        >[/color]

                        Bob Rock



                        Comment

                        • Bob Rock

                          #13
                          Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

                          > > Ahhh, so that is the way you can use the XmlArrayAttribu te and[color=blue][color=green]
                          > > XmlArrayItemAtt ribute on something that is not a field!!!
                          > > Is it possible to use it on methods returning arrays???[/color]
                          >
                          > Methods can return instances of Foo. Those instances of Foo will behave[/color]
                          as[color=blue]
                          > indicated by the attributes on Foo.
                          > If your method returns an array, then the attributes that apply are those[/color]
                          on[color=blue]
                          > the base type.
                          > So if it is an array of Foo that is being returned, then the attributes on
                          > the Foo definition will apply to the elements of the array.[/color]

                          The point here is not controlling the serialization of the elements of an
                          array, but of the array itself, in other words of the xml root element.
                          [color=blue]
                          > I believe it is not possible to effectively attribute arrays if they are[/color]
                          not[color=blue]
                          > serialized as members of classes. In other words, if it is a local[/color]
                          variable[color=blue]
                          > and you want to serialize it, I believe the attributes do not apply.[/color]

                          That is not true ... controlling serialization of a class field that is an
                          array is indeed very simple (XmlArrayAttrib ute and XmlArrayItemAtt ribute is
                          all that is needed).
                          [color=blue]
                          > Also
                          > if the array is a member variable and you do not serialize the entire[/color]
                          class,[color=blue]
                          > then I believe the attributes do not apply.
                          >[color=green]
                          > > I've also seen that there is a way of setting the root node element to
                          > > whatever one wants using the XmlSerializer(T ype, XmlRootAttribut e)
                          > > constructor. Unfortunately when deserializing I need to change the root[/color]
                          > node[color=green]
                          > > element to <ArrayOfClassna me> as the XmlSerializer expects to avoid the
                          > > exception.[/color]
                          >
                          > Yes this should work, but I don't understand what the problem is on
                          > de-serializing. If you are using the same kind of XmlSerializer
                          > (constructed with the XmlRootAttribut e), then the string or stream with[/color]
                          the[color=blue]
                          > modified root node should de-serialize just fine. eg[/color]

                          Again, this is not true. Using the XmlSerializer constructor with the
                          XmlRootAttribut e parameter lets you control only serialization ... it does
                          not make the XmlSerializer instance *understand* an xml with the root node
                          set to the XmlRootAttribut e parameter you are passing in when using that
                          XmlSerializer instance to deserialize an xml into the array. It *still*
                          expects the xml that it would generate normally (the one with
                          <ArrayOfClassna me>).
                          [color=blue]
                          >[color=green]
                          > >Sorry for the cross-posting, but it is done only in the hope that more[/color]
                          > people may read (and hopefully answer) my post.
                          >[/color]
                          [color=blue]
                          >. Yes, more people see it. But not more
                          > people who care, or who will have anything to contribute.[/color]

                          This is just your personal opinion.
                          I posted not on *any* NG, but on NGs that I felt are visited by people that
                          could have the answer I was looking for .... xml or web services newsgroups
                          (and I believe serialization is something that should be well known by
                          people dealing with WS).
                          [color=blue]
                          > You could also
                          > ask xml serialization questions to people you meet in the subway on the[/color]
                          way[color=blue]
                          > to work, or at church, or in the stands at the ballgame, but I doubt[/color]
                          you'll[color=blue]
                          > have much success there either.
                          >
                          > Imagine if everyone followed your policy. Every question on every topic
                          > would get asked on every group. Would that work? The different groups[/color]
                          are[color=blue]
                          > set up to raise the signal-to-noise ratio. If only works if the
                          > participants follow the conventions.
                          >
                          > Sometimes cross-posts make sense because it is not clear what area the
                          > question would fall under. 2 groups is reasonable. 3 is about the limit.
                          > 4 is egregious.
                          >
                          > I am not making any of this up. This is just standard netiquette.
                          > http://www.google.com/search?hl=en&n...+cross+posting
                          >
                          >
                          > -D
                          >[/color]

                          Bob Rock



                          Comment

                          • Ming Chen [.NET MVP]

                            #14
                            Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

                            Hi, Bob.
                            One solution I can think right out-of-the box is:

                            public class Classnames {
                            [XmlElement("Cla ssname")]
                            public Classname[] Members;
                            ......
                            }

                            By applying XmlElementAttri bute to an array, You can eliminate the
                            ArrayOfClassnam e/Members element from the serialized result. And by wrapping
                            the array in a class named Classnames, you can make sure the result Xml has
                            a Classnames root element. The net effect is that you get an Xml document
                            that has a Classnames root and a list of Classname.

                            On the other hand, .NET does allow XmlXXXAttribute s to be applied on
                            function return value. But there isn't any easy way to leverage it. E.g:
                            WebService engine utilizes this through XmlMapping, which is marked "not
                            intended to be used directly from your code" in MSDN. I think that you can
                            still use XmlMapping classes, though.

                            Hope this helps.
                            Ming Chen [.NET MVP]

                            "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
                            news:O3JZzQ7UEH A.808@tk2msftng p13.phx.gbl...[color=blue]
                            > Hello,
                            >
                            > when serializing an array of elements of a class Classname using
                            > XmlSerializer.S erialize() I get an XML like the following:
                            >
                            > <?xml version="1.0">
                            > <ArrayOfClassna me>
                            > ......
                            > ......
                            > </ArrayOfClassnam e>
                            >
                            > I'd like to be able to set the XML root node to something different from
                            > <ArrayOfClassna me> .... for example something like <Classnames>.
                            > As an alternative when deserilizing an XML such as the following:
                            >
                            > <?xml version="1.0">
                            > <Classnames>
                            > ......
                            > ......
                            > </Classnames>
                            >
                            > I'd like to be able to "load" it into an array of objects of class[/color]
                            Classname[color=blue]
                            > (at the moment when making such an attempt I get an obvious exception
                            > stating "<Classname s> was not expected").
                            >
                            >
                            > Bob Rock
                            >
                            >[/color]


                            Comment

                            • Ming Chen [.NET MVP]

                              #15
                              Re: Serialization with XmlSerializer: how to set the XML root node to something different from &lt;ArrayOfClas sname&gt;????

                              Hi, Bob.
                              One solution I can think right out-of-the box is:

                              public class Classnames {
                              [XmlElement("Cla ssname")]
                              public Classname[] Members;
                              ......
                              }

                              By applying XmlElementAttri bute to an array, You can eliminate the
                              ArrayOfClassnam e/Members element from the serialized result. And by wrapping
                              the array in a class named Classnames, you can make sure the result Xml has
                              a Classnames root element. The net effect is that you get an Xml document
                              that has a Classnames root and a list of Classname.

                              On the other hand, .NET does allow XmlXXXAttribute s to be applied on
                              function return value. But there isn't any easy way to leverage it. E.g:
                              WebService engine utilizes this through XmlMapping, which is marked "not
                              intended to be used directly from your code" in MSDN. I think that you can
                              still use XmlMapping classes, though.

                              Hope this helps.
                              Ming Chen [.NET MVP]

                              "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
                              news:O3JZzQ7UEH A.808@tk2msftng p13.phx.gbl...[color=blue]
                              > Hello,
                              >
                              > when serializing an array of elements of a class Classname using
                              > XmlSerializer.S erialize() I get an XML like the following:
                              >
                              > <?xml version="1.0">
                              > <ArrayOfClassna me>
                              > ......
                              > ......
                              > </ArrayOfClassnam e>
                              >
                              > I'd like to be able to set the XML root node to something different from
                              > <ArrayOfClassna me> .... for example something like <Classnames>.
                              > As an alternative when deserilizing an XML such as the following:
                              >
                              > <?xml version="1.0">
                              > <Classnames>
                              > ......
                              > ......
                              > </Classnames>
                              >
                              > I'd like to be able to "load" it into an array of objects of class[/color]
                              Classname[color=blue]
                              > (at the moment when making such an attempt I get an obvious exception
                              > stating "<Classname s> was not expected").
                              >
                              >
                              > Bob Rock
                              >
                              >[/color]


                              Comment

                              Working...