Attributes on a class to generate parameter values inside XML node

E

Erik Sargent

I might have the terms wrong, I'm an XML newbie, but here is what I
need to do:

I am returning an object in a webservice.

public class Profile
{
public String clinic1accountName;
public String clinic2accountName;
}

Which will return something like:
<Profile>
<clinic1accountName>1234</clinic1accountName>
<clinic2accountName>1234</clinic2accountName>
</Profile>


The trick is that each clinic has some additional metadata with it,
such as the business unit it belongs to (there are 40 clinics in 6
units) and I would like to return that as a property(?) of the node,
so it would look something like

<Profile>
<clinic1accountName org="1">1234</clinic1accountName>
<clinic2accountName org="2">1234</clinic2accountName>
</Profile>

I had guessed that I could do something with Attributes, but I haven't
found the right combination. One example was:

public class Profile
{
[System.Xml.Serialization.XmlAttribute(AttributeName = "org")]
public String clinic1accountName;
public String clinic2accountName;
}

But this eliminates accountNumber1 from the body and adds the "org" as
a property of the class' top node ("Profile"), rather than as a
property of "clinic1accountName".

<Profile org="1234">
<clinic2accountName org="2">1234</clinic2accountName>
</Profile>

I suppose there are other ways to pass the information along, but it
seemed to me that since the service knows this metadata and the
application doesn't and shouldn't, but needs to pass the value along
in some cases, that I should try to keep the information in the XML
data itself, rather than try to teach the application how to fetch it
and associate it with each account.

By the way, the org value is always the same for each
clinicAccountName, so clinic14 will always be org=5.

Thank you for reading and any ideas you may have.
 
D

Dan Rogers

Hi Erik,

The right way to do this depends on how static you expect the number and
name of the attributes to be, and whether or not you want to be able to
process any of the data the callers are passing.

In general, for data that you want the service to consume and be able to
process, you should provide a named property on your class. In your second
example you tried to use one property to do double duty, and the result was
that you simply turned it into an attribute with a different name.

To handle let's say five attributes whose name you know (so that you can
then write code to process them), you would create five additional
fields/properties on your class, and place a separate XmlAttributeAttribute
on each one. This way you'd get these properties as XML attributes during
serialization, and still have your main fields as elements.

Another possible interpretation of your request is you want a way to handle
any number of attributes, without having to know all of their names. In
this case, you should make a string array property and decorate it with an
XmlAnyAttributeAttribute. This way, any attribute that is received will be
placed into the array of objects. These will all, at runtime, be XmlNode
objects that allow you to examine the attribute name and value.

I hope this helps,

Dan Rogers (danro)
Microsoft Corporation
--------------------
From: (e-mail address removed) (Erik Sargent)
Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
Subject: Attributes on a class to generate parameter values inside XML node
Date: 5 Nov 2004 16:41:26 -0800
Organization: http://groups.google.com
Lines: 57
Message-ID: <[email protected]>
NNTP-Posting-Host: 170.220.2.13
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1099701686 3249 127.0.0.1 (6 Nov 2004 00:41:26 GMT)
X-Complaints-To: (e-mail address removed)
NNTP-Posting-Date: Sat, 6 Nov 2004 00:41:26 +0000 (UTC)
Path: cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!tornado.fastwebnet.it!tiscali!new
sfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!t-online.de!news.zanker.org
!news.cs.univ-paris8.fr!proxad.net!216.239.36.134.MISMATCH!postnews.google.c
om!not-for-mail
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.aspnet.webservices:26401
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices

I might have the terms wrong, I'm an XML newbie, but here is what I
need to do:

I am returning an object in a webservice.

public class Profile
{
public String clinic1accountName;
public String clinic2accountName;
}

Which will return something like:
<Profile>
<clinic1accountName>1234</clinic1accountName>
<clinic2accountName>1234</clinic2accountName>
</Profile>


The trick is that each clinic has some additional metadata with it,
such as the business unit it belongs to (there are 40 clinics in 6
units) and I would like to return that as a property(?) of the node,
so it would look something like

<Profile>
<clinic1accountName org="1">1234</clinic1accountName>
<clinic2accountName org="2">1234</clinic2accountName>
</Profile>

I had guessed that I could do something with Attributes, but I haven't
found the right combination. One example was:

public class Profile
{
[System.Xml.Serialization.XmlAttribute(AttributeName = "org")]
public String clinic1accountName;
public String clinic2accountName;
}

But this eliminates accountNumber1 from the body and adds the "org" as
a property of the class' top node ("Profile"), rather than as a
property of "clinic1accountName".

<Profile org="1234">
<clinic2accountName org="2">1234</clinic2accountName>
</Profile>

I suppose there are other ways to pass the information along, but it
seemed to me that since the service knows this metadata and the
application doesn't and shouldn't, but needs to pass the value along
in some cases, that I should try to keep the information in the XML
data itself, rather than try to teach the application how to fetch it
and associate it with each account.

By the way, the org value is always the same for each
clinicAccountName, so clinic14 will always be org=5.

Thank you for reading and any ideas you may have.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top