469,332 Members | 6,843 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

XmlTextWriter - Attributes

Hello:

I would like to output the following XML Element using the XmlTextWriter,
but I cannot get it correct:
-----------------------------------------------------------------------------------------------
<SOAP-ENV:Body
xmlns:SOAP-SEC="http://schemas.xmlsoap.org/soap/security/2000-12"
SOAP-SEC:id="body">
-----------------------------------------------------------------------------------------------

I have tried the following:
xw.WriteStartElement("SOAP-ENV", "Body", "123");
xw.WriteAttributeString("xmlns", "SOAP-SEC", null,
"http://schemas.xmlsoap.org/soap/security/2000-12");
//xw.WriteAttributeString("SOAP-SEC", "id", null, "body");
xw.WriteAttributeString("SOAP-SEC", "id", "123", "body");

And nothing has worked. I keep getting extra attributes in my element.
What am I missing?

Thanks
Andy

Nov 12 '05 #1
2 1920
"Andy" <An**@discussions.microsoft.com> wrote in message news:D4**********************************@microsof t.com...
<SOAP-ENV:Body
xmlns:SOAP-SEC="http://schemas.xmlsoap.org/soap/security/2000-12"
SOAP-SEC:id="body">
Presumably SOAP-ENV was declared on a parent element, like Envelope,
so it should be possible to produce this literal XML.
I have tried the following:
xw.WriteStartElement("SOAP-ENV", "Body", "123");
What is it that "123" is supposed to mean?

Assuming the conventional definition of SOAP-ENV, this will work,

// WriteStartElement( prefix, localName, namespaceUri);
xw.WriteStartElement( "SOAP-ENV", "Body", "http://schemas.xmlsoap.org/soap/envelope/");

When xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" is
already declared on a parent element, no xmlns declaration will be written
for the SOAP-ENV:Body tag. However, if this prefix hasn't been declared
yet then XmlWriter will emit it now.

There's no reason to call,
xw.WriteAttributeString("xmlns", "SOAP-SEC", null,
"http://schemas.xmlsoap.org/soap/security/2000-12");
to write an xmlns declaration -- again, the XmlWriter will emit an
xmlns declaration only when it is required (as long as you pass the
correct namespace URI for the prefix when you call
WriteAttributeString( )).
xw.WriteAttributeString("SOAP-SEC", "id", "123", "body");
Hence, this line should become,

// WriteAttributeString( prefix, localName, namespaceURI, attributeValue)
xw.WriteAttributeString( "SOAP-SEC", "id",
"http://schemas.xmlsoap.org/soap/security/2000-12", "body");
And nothing has worked. I keep getting extra attributes in my element.
What am I missing?


Only those two lines (and a WriteEndElement( )) are needed. Don't
stress over the xmlns declarations, XmlWriter will handle those for
you automatically if you pass the correct prefixes/namespace URIs
to the WriteStartElement( ) and WriteAttributeString( ) methods.
Derek Harmon
Nov 12 '05 #2
Derek:

Thank you very much for the help. This is exactly what I needed.

To answer your question about the "123": I simply found that example on a
Microsoft webpage, and it was something I was trying. It said the 123 would
be overwritten.

"Derek Harmon" wrote:
"Andy" <An**@discussions.microsoft.com> wrote in message news:D4**********************************@microsof t.com...
<SOAP-ENV:Body
xmlns:SOAP-SEC="http://schemas.xmlsoap.org/soap/security/2000-12"
SOAP-SEC:id="body">


Presumably SOAP-ENV was declared on a parent element, like Envelope,
so it should be possible to produce this literal XML.
I have tried the following:
xw.WriteStartElement("SOAP-ENV", "Body", "123");


What is it that "123" is supposed to mean?

Assuming the conventional definition of SOAP-ENV, this will work,

// WriteStartElement( prefix, localName, namespaceUri);
xw.WriteStartElement( "SOAP-ENV", "Body", "http://schemas.xmlsoap.org/soap/envelope/");

When xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" is
already declared on a parent element, no xmlns declaration will be written
for the SOAP-ENV:Body tag. However, if this prefix hasn't been declared
yet then XmlWriter will emit it now.

There's no reason to call,
xw.WriteAttributeString("xmlns", "SOAP-SEC", null,
"http://schemas.xmlsoap.org/soap/security/2000-12");


to write an xmlns declaration -- again, the XmlWriter will emit an
xmlns declaration only when it is required (as long as you pass the
correct namespace URI for the prefix when you call
WriteAttributeString( )).
xw.WriteAttributeString("SOAP-SEC", "id", "123", "body");


Hence, this line should become,

// WriteAttributeString( prefix, localName, namespaceURI, attributeValue)
xw.WriteAttributeString( "SOAP-SEC", "id",
"http://schemas.xmlsoap.org/soap/security/2000-12", "body");
And nothing has worked. I keep getting extra attributes in my element.
What am I missing?


Only those two lines (and a WriteEndElement( )) are needed. Don't
stress over the xmlns declarations, XmlWriter will handle those for
you automatically if you pass the correct prefixes/namespace URIs
to the WriteStartElement( ) and WriteAttributeString( ) methods.
Derek Harmon

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Lee Atkinson | last post: by
reply views Thread by Andrew R. Thomas-Cramer | last post: by
4 posts views Thread by Einar HÝst | last post: by
1 post views Thread by Karl Hungus | last post: by
4 posts views Thread by John Salerno | last post: by
1 post views Thread by leonard.guillaume | last post: by
2 posts views Thread by fmancina | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.