what is the purpose of this declarative [XmlRootAttribute.....] ?

  • Thread starter Thread starter hazz
  • Start date Start date
H

hazz

I just haven't been able to get a solid feel for the purpose of what appears
to be a declarative statement just above the class below. Anyone with a
clear explanation that will finally stick with me? What is the url for?
If I go into Visual Studio .NET help, what is it I look for? thx, -hazz

namespace Appellation
{

[XmlRootAttribute("UserNameToken",Namespace="http://www.moi.com/Internal/Tok
en/username/1.0")]
public class UserNameToken : Token
 
hazz,

What you are seeing is an attribute. It is a way of adding data to the
type that you are declaring (or rather a member of a type). You would, at
runtime, be able to get those pieces of data associated with the type
(metadata), and perform actions on them, if you wish.

In this case, the XmlRootAttribute is what you would be looking for. In
this case, when the class is serialized using the XmlSerializer, the
serializer can tell from the class that it wants the root element to be
"UserNameToken", and that the root element has the namespace defined in
Namespace.

For more information, check out the section of the .NET framework
documentation titled "Extending Metadata Using Attributes", located at
(watch for line wrap):

http://msdn.microsoft.com/library/d...tml/cpconextendingmetadatausingattributes.asp

Hope this helps.
 
This is used to provide some control over XML serialization of this class. It
tells the serialization code that the root node of the generated XML file
will be called "UserNameToken" and that it will belong to the XML namespace
(not to be confused with the .NET namespace)
"http://www.moi.com/Internal/Token/username/1.0".

Note that the XML namespace is an arbitrary URL which makes the declaration
of UserNameToken unique (in much the same was as Appellation.UserNameToken)
in the XML world.

The declaration shown will ensure that your generated XML have a root node
which is something like:

<?xml version="1.0" encoding="utf-8"?>
<UserNameToken xmlns="http://www.moi.com/Internal/Token/username/1.0">
....
</UserNameToken>

Hopefully this makes things clearer!
Chris.
 
Thanks alot Nicholas. I get it. And thank you for the link.
-greg

Nicholas Paldino said:
hazz,

What you are seeing is an attribute. It is a way of adding data to the
type that you are declaring (or rather a member of a type). You would, at
runtime, be able to get those pieces of data associated with the type
(metadata), and perform actions on them, if you wish.

In this case, the XmlRootAttribute is what you would be looking for. In
this case, when the class is serialized using the XmlSerializer, the
serializer can tell from the class that it wants the root element to be
"UserNameToken", and that the root element has the namespace defined in
Namespace.

For more information, check out the section of the .NET framework
documentation titled "Extending Metadata Using Attributes", located at
(watch for line wrap):

http://msdn.microsoft.com/library/d...tml/cpconextendingmetadatausingattributes.asp

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

hazz said:
I just haven't been able to get a solid feel for the purpose of what
appears
to be a declarative statement just above the class below. Anyone with a
clear explanation that will finally stick with me? What is the url for?
If I go into Visual Studio .NET help, what is it I look for? thx, -hazz

namespace Appellation
{

[XmlRootAttribute("UserNameToken",Namespace="http://www.moi.com/Internal/Tok
en/username/1.0")]
public class UserNameToken : Token
 
Thank you for that namespace distinction Chris. That had thrown me off.
And thank you for the sample xml syn tax to drive the point home.
-Greg


Chris Ballard said:
This is used to provide some control over XML serialization of this class. It
tells the serialization code that the root node of the generated XML file
will be called "UserNameToken" and that it will belong to the XML namespace
(not to be confused with the .NET namespace)
"http://www.moi.com/Internal/Token/username/1.0".

Note that the XML namespace is an arbitrary URL which makes the declaration
of UserNameToken unique (in much the same was as Appellation.UserNameToken)
in the XML world.

The declaration shown will ensure that your generated XML have a root node
which is something like:

<?xml version="1.0" encoding="utf-8"?>
<UserNameToken xmlns="http://www.moi.com/Internal/Token/username/1.0">
...
</UserNameToken>

Hopefully this makes things clearer!
Chris.

hazz said:
I just haven't been able to get a solid feel for the purpose of what appears
to be a declarative statement just above the class below. Anyone with a
clear explanation that will finally stick with me? What is the url for?
If I go into Visual Studio .NET help, what is it I look for? thx, -hazz

namespace Appellation
{

[XmlRootAttribute("UserNameToken",Namespace="http://www.moi.com/Internal/Tok
en/username/1.0")]
public class UserNameToken : Token
 
Back
Top