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
  • Daniel Cazzulino [MVP XML]

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

    You can use:

    [WebMethod]
    [return: XmlArray("Custo mers")]
    public Customer[] GetCustomers()

    HTH

    --
    Daniel Cazzulino [MVP XML]
    Clarius Consulting SA



    "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
    news:eEQ$5gEVEH A.1656@TK2MSFTN GP09.phx.gbl...[color=blue][color=green][color=darkred]
    > > > 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???[/color]
    > >
    > > Methods can return instances of Foo. Those instances of Foo will behave[/color]
    > as[color=green]
    > > indicated by the attributes on Foo.
    > > If your method returns an array, then the attributes that apply are[/color][/color]
    those[color=blue]
    > on[color=green]
    > > the base type.
    > > So if it is an array of Foo that is being returned, then the attributes[/color][/color]
    on[color=blue][color=green]
    > > 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=green]
    > > I believe it is not possible to effectively attribute arrays if they are[/color]
    > not[color=green]
    > > serialized as members of classes. In other words, if it is a local[/color]
    > variable[color=green]
    > > 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[/color]
    is[color=blue]
    > all that is needed).
    >[color=green]
    > > Also
    > > if the array is a member variable and you do not serialize the entire[/color]
    > class,[color=green]
    > > then I believe the attributes do not apply.
    > >[color=darkred]
    > > > 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[/color][/color][/color]
    root[color=blue][color=green]
    > > node[color=darkred]
    > > > element to <ArrayOfClassna me> as the XmlSerializer expects to avoid[/color][/color][/color]
    the[color=blue][color=green][color=darkred]
    > > > 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=green]
    > > 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=green]
    > >[color=darkred]
    > > >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=green]
    > >. 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[/color]
    that[color=blue]
    > could have the answer I was looking for .... xml or web services[/color]
    newsgroups[color=blue]
    > (and I believe serialization is something that should be well known by
    > people dealing with WS).
    >[color=green]
    > > You could also
    > > ask xml serialization questions to people you meet in the subway on the[/color]
    > way[color=green]
    > > to work, or at church, or in the stands at the ballgame, but I doubt[/color]
    > you'll[color=green]
    > > 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=green]
    > > 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[/color][/color]
    limit.[color=blue][color=green]
    > > 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
    >
    >
    >[/color]


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.708 / Virus Database: 464 - Release Date: 18/06/2004


    Comment

    • Daniel Cazzulino [MVP XML]

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

      You can use:

      [WebMethod]
      [return: XmlArray("Custo mers")]
      public Customer[] GetCustomers()

      HTH

      --
      Daniel Cazzulino [MVP XML]
      Clarius Consulting SA



      "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
      news:eEQ$5gEVEH A.1656@TK2MSFTN GP09.phx.gbl...[color=blue][color=green][color=darkred]
      > > > 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???[/color]
      > >
      > > Methods can return instances of Foo. Those instances of Foo will behave[/color]
      > as[color=green]
      > > indicated by the attributes on Foo.
      > > If your method returns an array, then the attributes that apply are[/color][/color]
      those[color=blue]
      > on[color=green]
      > > the base type.
      > > So if it is an array of Foo that is being returned, then the attributes[/color][/color]
      on[color=blue][color=green]
      > > 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=green]
      > > I believe it is not possible to effectively attribute arrays if they are[/color]
      > not[color=green]
      > > serialized as members of classes. In other words, if it is a local[/color]
      > variable[color=green]
      > > 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[/color]
      is[color=blue]
      > all that is needed).
      >[color=green]
      > > Also
      > > if the array is a member variable and you do not serialize the entire[/color]
      > class,[color=green]
      > > then I believe the attributes do not apply.
      > >[color=darkred]
      > > > 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[/color][/color][/color]
      root[color=blue][color=green]
      > > node[color=darkred]
      > > > element to <ArrayOfClassna me> as the XmlSerializer expects to avoid[/color][/color][/color]
      the[color=blue][color=green][color=darkred]
      > > > 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=green]
      > > 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=green]
      > >[color=darkred]
      > > >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=green]
      > >. 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[/color]
      that[color=blue]
      > could have the answer I was looking for .... xml or web services[/color]
      newsgroups[color=blue]
      > (and I believe serialization is something that should be well known by
      > people dealing with WS).
      >[color=green]
      > > You could also
      > > ask xml serialization questions to people you meet in the subway on the[/color]
      > way[color=green]
      > > to work, or at church, or in the stands at the ballgame, but I doubt[/color]
      > you'll[color=green]
      > > 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=green]
      > > 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[/color][/color]
      limit.[color=blue][color=green]
      > > 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
      >
      >
      >[/color]


      ---
      Outgoing mail is certified Virus Free.
      Checked by AVG anti-virus system (http://www.grisoft.com).
      Version: 6.0.708 / Virus Database: 464 - Release Date: 18/06/2004


      Comment

      Working...