471,573 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,573 software developers and data experts.

Setting Namespace Prefix on an Element...

I'm using XML serialization to produce the following XML document:

<TestDoc xmlns:srd="some-url">
<Additional>
<Security>
<srd:Login>login_id</srd:Login>
<srd:Password>password</srd:Password>
</Security>
</Additional>
</TestDoc>

from this class:
[XmlType(TypeName="SRD3", Namespace="urn:srd-3.0")]
public class Security
{
public Security()
{
}

#region Public Properties
public string Login
{
get { return m_sLogin; }
set { m_sLogin = value; }
}

public string Password
{
get { return m_sPassword; }
set { m_sPassword = value; }
}
#endregion

#region Fields
private string m_sLogin;
private string m_sPassword;
#endregion
}

The Security class is actually contained in another class via an ArrayList
that is serialized with this Attribute:

[XmlArray("Additional"), XmlArrayItem(typeof(Security))]

However, I need the XML document to look as follows:

<TestDoc xmlns:srd="some-url">
<Additional>
<srd:Security>
<srd:Login>login_id</srd:Login>
<srd:Password>password</srd:Password>
</srd:Security>
</Additional>
</TestDoc>

Any thoughts on why the Namespace prefix is not being applied to the
Security element?

Thanks
Nov 12 '05 #1
4 2468
Hollywood,

The XmlSerializer doesn't "see" the XmlType attribute because the
<Sercurity> element is not at the root of the serialized object graph. You
need to attach an XmlElement attribute to the Security field in the
Additional class. This will still not produce any namespace prefixes. If you
need the prefixes to be srd, then take a look at [0].

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[0] http://www.topxml.com/xmlserializer/...at_runtime.asp

"Hollywood" <ho*******@thzero.com> wrote in message
news:#Y**************@TK2MSFTNGP11.phx.gbl...
I'm using XML serialization to produce the following XML document:

<TestDoc xmlns:srd="some-url">
<Additional>
<Security>
<srd:Login>login_id</srd:Login>
<srd:Password>password</srd:Password>
</Security>
</Additional>
</TestDoc>

from this class:
[XmlType(TypeName="SRD3", Namespace="urn:srd-3.0")]
public class Security
{
public Security()
{
}

#region Public Properties
public string Login
{
get { return m_sLogin; }
set { m_sLogin = value; }
}

public string Password
{
get { return m_sPassword; }
set { m_sPassword = value; }
}
#endregion

#region Fields
private string m_sLogin;
private string m_sPassword;
#endregion
}

The Security class is actually contained in another class via an ArrayList
that is serialized with this Attribute:

[XmlArray("Additional"), XmlArrayItem(typeof(Security))]

However, I need the XML document to look as follows:

<TestDoc xmlns:srd="some-url">
<Additional>
<srd:Security>
<srd:Login>login_id</srd:Login>
<srd:Password>password</srd:Password>
</srd:Security>
</Additional>
</TestDoc>

Any thoughts on why the Namespace prefix is not being applied to the
Security element?

Thanks

Nov 12 '05 #2
Christoph,

Only problem with that is that the "Additional" class is in really just an
ArrayList, contained in the TestDoc class, that contains the Security
objects.

Not to mention, using a namespace in the Serializer is no good, since a)
TestDoc is not in the "srd" namepsace and b) there are additional TestDoc
fields that will be outputted but not mentioned as I didn't think they'd be
relevant.

Essentially, what I need is for the Security element and its sub-elements to
ALL be part of the "srd" namespace.

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:eA**************@TK2MSFTNGP12.phx.gbl...
Hollywood,

The XmlSerializer doesn't "see" the XmlType attribute because the
<Sercurity> element is not at the root of the serialized object graph. You
need to attach an XmlElement attribute to the Security field in the
Additional class. This will still not produce any namespace prefixes. If you need the prefixes to be srd, then take a look at [0].

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[0] http://www.topxml.com/xmlserializer/...at_runtime.asp

"Hollywood" <ho*******@thzero.com> wrote in message
news:#Y**************@TK2MSFTNGP11.phx.gbl...
I'm using XML serialization to produce the following XML document:

<TestDoc xmlns:srd="some-url">
<Additional>
<Security>
<srd:Login>login_id</srd:Login>
<srd:Password>password</srd:Password>
</Security>
</Additional>
</TestDoc>

from this class:
[XmlType(TypeName="SRD3", Namespace="urn:srd-3.0")]
public class Security
{
public Security()
{
}

#region Public Properties
public string Login
{
get { return m_sLogin; }
set { m_sLogin = value; }
}

public string Password
{
get { return m_sPassword; }
set { m_sPassword = value; }
}
#endregion

#region Fields
private string m_sLogin;
private string m_sPassword;
#endregion
}

The Security class is actually contained in another class via an ArrayList that is serialized with this Attribute:

[XmlArray("Additional"), XmlArrayItem(typeof(Security))]

However, I need the XML document to look as follows:

<TestDoc xmlns:srd="some-url">
<Additional>
<srd:Security>
<srd:Login>login_id</srd:Login>
<srd:Password>password</srd:Password>
</srd:Security>
</Additional>
</TestDoc>

Any thoughts on why the Namespace prefix is not being applied to the
Security element?

Thanks


Nov 12 '05 #3
In that case you need to replace the XmlElementAttribute with an
XmlArrayItem to add the namespace declaration for the enclosing array
element and an XmlArrayItem attribute to declare the type and the namespace
of the elements in the array.

With attributes in place like this:
public class Parent
{
[XmlArray("Additional", Namespace="urn:foo" )]
[XmlArrayItem( typeof(Child), Namespace="urn:foo" )]
public ArrayList Children;
}
public class Child
{
public string field = "foo";
}

the serialized output will have the namespace delcarations you need:

<Parent xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Additional xmlns="urn:foo">
<Child>
<field>foo</field>
</Child>
</Additional>
</Parent>
--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Hollywood" <ho************@thzero.com> wrote in message
news:uL*************@TK2MSFTNGP12.phx.gbl...
Christoph,

Only problem with that is that the "Additional" class is in really just an
ArrayList, contained in the TestDoc class, that contains the Security
objects.

Not to mention, using a namespace in the Serializer is no good, since a)
TestDoc is not in the "srd" namepsace and b) there are additional TestDoc
fields that will be outputted but not mentioned as I didn't think they'd be relevant.

Essentially, what I need is for the Security element and its sub-elements to ALL be part of the "srd" namespace.

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:eA**************@TK2MSFTNGP12.phx.gbl...
Hollywood,

The XmlSerializer doesn't "see" the XmlType attribute because the
<Sercurity> element is not at the root of the serialized object graph. You
need to attach an XmlElement attribute to the Security field in the
Additional class. This will still not produce any namespace prefixes. If

you
need the prefixes to be srd, then take a look at [0].

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[0] http://www.topxml.com/xmlserializer/...at_runtime.asp

"Hollywood" <ho*******@thzero.com> wrote in message
news:#Y**************@TK2MSFTNGP11.phx.gbl...
I'm using XML serialization to produce the following XML document:

<TestDoc xmlns:srd="some-url">
<Additional>
<Security>
<srd:Login>login_id</srd:Login>
<srd:Password>password</srd:Password>
</Security>
</Additional>
</TestDoc>

from this class:
[XmlType(TypeName="SRD3", Namespace="urn:srd-3.0")]
public class Security
{
public Security()
{
}

#region Public Properties
public string Login
{
get { return m_sLogin; }
set { m_sLogin = value; }
}

public string Password
{
get { return m_sPassword; }
set { m_sPassword = value; }
}
#endregion

#region Fields
private string m_sLogin;
private string m_sPassword;
#endregion
}

The Security class is actually contained in another class via an

ArrayList that is serialized with this Attribute:

[XmlArray("Additional"), XmlArrayItem(typeof(Security))]

However, I need the XML document to look as follows:

<TestDoc xmlns:srd="some-url">
<Additional>
<srd:Security>
<srd:Login>login_id</srd:Login>
<srd:Password>password</srd:Password>
</srd:Security>
</Additional>
</TestDoc>

Any thoughts on why the Namespace prefix is not being applied to the
Security element?

Thanks



Nov 12 '05 #4
Ah, yes... missed that. Thanks, worked perfectly!

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:ea**************@TK2MSFTNGP11.phx.gbl...
In that case you need to replace the XmlElementAttribute with an
XmlArrayItem to add the namespace declaration for the enclosing array
element and an XmlArrayItem attribute to declare the type and the namespace of the elements in the array.

With attributes in place like this:
public class Parent
{
[XmlArray("Additional", Namespace="urn:foo" )]
[XmlArrayItem( typeof(Child), Namespace="urn:foo" )]
public ArrayList Children;
}
public class Child
{
public string field = "foo";
}

the serialized output will have the namespace delcarations you need:

<Parent xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Additional xmlns="urn:foo">
<Child>
<field>foo</field>
</Child>
</Additional>
</Parent>
--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Hollywood" <ho************@thzero.com> wrote in message
news:uL*************@TK2MSFTNGP12.phx.gbl...
Christoph,

Only problem with that is that the "Additional" class is in really just an
ArrayList, contained in the TestDoc class, that contains the Security
objects.

Not to mention, using a namespace in the Serializer is no good, since a)
TestDoc is not in the "srd" namepsace and b) there are additional TestDoc fields that will be outputted but not mentioned as I didn't think they'd be
relevant.

Essentially, what I need is for the Security element and its sub-elements to
ALL be part of the "srd" namespace.

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote

in message news:eA**************@TK2MSFTNGP12.phx.gbl...
Hollywood,

The XmlSerializer doesn't "see" the XmlType attribute because the
<Sercurity> element is not at the root of the serialized object graph. You need to attach an XmlElement attribute to the Security field in the
Additional class. This will still not produce any namespace prefixes.

If you
need the prefixes to be srd, then take a look at [0].

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[0] http://www.topxml.com/xmlserializer/...at_runtime.asp

"Hollywood" <ho*******@thzero.com> wrote in message
news:#Y**************@TK2MSFTNGP11.phx.gbl...
> I'm using XML serialization to produce the following XML document:
>
> <TestDoc xmlns:srd="some-url">
> <Additional>
> <Security>
> <srd:Login>login_id</srd:Login>
> <srd:Password>password</srd:Password>
> </Security>
> </Additional>
> </TestDoc>
>
> from this class:
> [XmlType(TypeName="SRD3", Namespace="urn:srd-3.0")]
> public class Security
> {
> public Security()
> {
> }
>
> #region Public Properties
> public string Login
> {
> get { return m_sLogin; }
> set { m_sLogin = value; }
> }
>
> public string Password
> {
> get { return m_sPassword; }
> set { m_sPassword = value; }
> }
> #endregion
>
> #region Fields
> private string m_sLogin;
> private string m_sPassword;
> #endregion
> }
>
> The Security class is actually contained in another class via an

ArrayList
> that is serialized with this Attribute:
>
> [XmlArray("Additional"), XmlArrayItem(typeof(Security))]
>
> However, I need the XML document to look as follows:
>
> <TestDoc xmlns:srd="some-url">
> <Additional>
> <srd:Security>
> <srd:Login>login_id</srd:Login>
> <srd:Password>password</srd:Password>
> </srd:Security>
> </Additional>
> </TestDoc>
>
> Any thoughts on why the Namespace prefix is not being applied to the
> Security element?
>
> Thanks
>
>



Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by John L. Clark | last post: by
25 posts views Thread by kj | last post: by
3 posts views Thread by Mike Dickens | last post: by
4 posts views Thread by Krishna Tulasi via .NET 247 | last post: by
2 posts views Thread by Dale Anderson | last post: by
5 posts views Thread by David Thielen | last post: by
13 posts views Thread by Axel Dahmen | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.