473,574 Members | 3,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteStartEl ement("SOAP-ENV", "Body", "123");
xw.WriteAttribu teString("xmlns ", "SOAP-SEC", null,
"http://schemas.xmlsoap .org/soap/security/2000-12");
//xw.WriteAttribu teString("SOAP-SEC", "id", null, "body");
xw.WriteAttribu teString("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 2042
"Andy" <An**@discussio ns.microsoft.co m> wrote in message news:D4******** *************** ***********@mic rosoft.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.WriteStartEl ement("SOAP-ENV", "Body", "123");
What is it that "123" is supposed to mean?

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

// WriteStartEleme nt( prefix, localName, namespaceUri);
xw.WriteStartEl ement( "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.WriteAttribu teString("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
WriteAttributeS tring( )).
xw.WriteAttribu teString("SOAP-SEC", "id", "123", "body");
Hence, this line should become,

// WriteAttributeS tring( prefix, localName, namespaceURI, attributeValue)
xw.WriteAttribu teString( "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 WriteStartEleme nt( ) and WriteAttributeS tring( ) 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**@discussio ns.microsoft.co m> wrote in message news:D4******** *************** ***********@mic rosoft.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.WriteStartEl ement("SOAP-ENV", "Body", "123");


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

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

// WriteStartEleme nt( prefix, localName, namespaceUri);
xw.WriteStartEl ement( "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.WriteAttribu teString("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
WriteAttributeS tring( )).
xw.WriteAttribu teString("SOAP-SEC", "id", "123", "body");


Hence, this line should become,

// WriteAttributeS tring( prefix, localName, namespaceURI, attributeValue)
xw.WriteAttribu teString( "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 WriteStartEleme nt( ) and WriteAttributeS tring( ) methods.
Derek Harmon

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3354
by: T Conti | last post by:
Howdy: I am currently writing a .Net handler to replace an ASP page. The ASP module used a COM dll to format XML. This dll was pretty robust and it auto-suppressed empty elements and attributes. A developer in my group is porting the code into .Net, but I an still believe that there is a way to do this in the native .Net classes. Is...
1
2875
by: Lee Atkinson | last post by:
Hi - I am trying to read an XML file using XmlTextReader and write it out using XmlTextWriter. The XML is XHTML. At the moment, I am trying to create a straightforward copy of a file. The XHTML file contains nodes and attributes (with there own namespace) that will later be used to alter the XML into a desired output. This 'copying' is the...
0
1490
by: Andrew R. Thomas-Cramer | last post by:
I'm using XMLTextWriter to write to a socket stream. I need to write the opening root tag immediately for receipt by the client; elements beneath it will come later. m_writer.WriteStartDocument(); m_writer.WriteStartElement( "stream", "stream", "mynamespace" ); m_writer.Flush(); What I want is this:
4
9281
by: Einar Høst | last post by:
Hi, I'm having weird problems using StringWriter and XmlTextWriter. My code looks like this: StringWriter sw = new StringWriter(CultureInfo.InvariantInfo); XmlTextWriter xtw = new XmlTextWriter(sw); // NullReferenceException thrown here! xtw.WriteStartDocument(); xtw.WriteStartElement("MyRoot");
1
2025
by: Karl Hungus | last post by:
Is it possible to use XmlTextReader and XmlTextWriter to open an XML file, search for a particular node, and then append nodes there? If so, do you have a little code example (c#)? Thanks in advance, Karl
4
5861
by: John Salerno | last post by:
I thought I might use the XML functions in C# to help me do some repetitive typing in an XHTML file, but I'm stuck. Here's what I have before I just stopped: void WriteXMLFile() { string path = @"C:\elements.xml"; XmlTextWriter xml = new XmlTextWriter(path, System.Text.Encoding.UTF8);
1
1292
by: leonard.guillaume | last post by:
Hi guys, I have a task to do where I need to write an XML file into SOAP format. I use XMLTextWriter in order to write my strings to the XML file, but I must convert somehow this xml into SOAP. How can I do that ? Is there a way to do it without hardcoding SOAP attributes and elements ? As exemple, a function that would receive a stream and...
2
5639
by: fmancina | last post by:
Hi, I am employing the XmlTextWriter class to generate an XML document. Everything works fine, until I have to write an attribute to an element which contains a value. Examples below: // <Author>Frank</Author> // is equivalent to
0
1245
by: pierreuk | last post by:
Hi All, I have been looking at the best way of doing this but can't find a solution that doesn't involve using bytes array. Basically I want to wrap a XmlTextWriter in a GZipStream so I produce a GZipped Xml file. I have been trying this: using (GZipStream gzipout = new GZipStream(File.Create(Path), CompressionMode.Compress)) {
0
7758
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8100
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8270
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6509
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5651
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1099
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.