473,236 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,236 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 2580
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a...
25
by: kj | last post by:
Consider the following XML document: <?xml version='1.0' encoding='UTF-8'?> <bar:foo xmlns:bar='someuri'> <baz/> </bar:foo> What namespace does baz belong to? What is this namespace bound...
3
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0"...
4
by: Krishna Tulasi via .NET 247 | last post by:
Hi, I am having trouble with creation of XML programmatically using .NET. Specifically Im trying to create an element which looks like below and insert into an existing xml doc: <Worksheet...
2
by: Dale Anderson | last post by:
I have a schema that I'm trying to read. The schema has an element named 'GrantApplication' and one with a namespace prefix named 'SF424:GrantApplication'. When I try to read this schema in, I...
6
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a...
5
by: David Thielen | last post by:
Hi; I set up my xml as follows: XmlDocument xml = new XmlDocument(); xml.Load(File.Open("data.xml", FileMode.Open, FileAccess.Read)); XmlNamespaceManager context = new...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
13
by: Axel Dahmen | last post by:
Hi, I've got a question on namespaces. After reading http://www.w3.org/TR/xml-names11 I still don't understand how namespaces are applied to attributes - particularly in regard to how processing...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.