473,397 Members | 2,099 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,397 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 2591
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.