views:

82

answers:

2

I am trying to create an asp.net web form that allows a user to enter information, then have this information sent via an XMLwriter to a web service.

Here is a snippet of the xml as it should be outputted;

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
 <soap:Body xmlns:ns1="http://its/foo.wsdl"&gt;

I try to manipulate this via code;

xml.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")     
xml.WriteAttributeString("xmlns", "ns1", "http://its/foo.wsdl")

But I get this error:

The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'.

Can anyone tell me what I am doing wrong?

Thanks.

A: 

Briefly, the 'xmlns' "attributes" are not real attributes. They are namespace declarations. You do not need to generate them. They will be generated, as necessary, as part of the generation of your content into different XML namespaces.

John Saunders
Thanks, but what do I need to do?
Phil
Ignore them and do everything else.
John Saunders
A: 
using (var writer = XmlWriter.Create(Console.Out))
{
    writer.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
    writer.WriteStartElement("soap", "Body", null);
    writer.WriteAttributeString("xmlns", "ns1", null, "http://its/foo.wsdl");
    // ... add other tags
    writer.WriteEndElement();
    writer.WriteEndElement();
}
Darin Dimitrov
@Darin, I don't believe he needs to explicitly create `xmlns:ns1`
John Saunders
Thanks, using this code I get errors on the nulls, saying use system.dbnull instead which also causes an error
Phil
John, can you give me more info on why i dont need to explicitly create xmlns:ns1 ? and how the xml above can be generated without please.
Phil
@Phil, the snippet I provided generates exactly the XML you are expecting.
Darin Dimitrov
Thanks Darin, I was a bit thrown by the C# :0) . Now working.
Phil