I am trying to create an xml with the following code.
XmlDocument xmlDocument = new XmlDocument();
XmlProcessingInstruction xPI = xmlDocument.CreateProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlDocument.AppendChild(xPI);
XmlElement xElmntheader = xmlDocument.CreateElement("soapenv:Header", " ");
xmlDocument.AppendChild(xElmntheader);
MemoryStream ms = new MemoryStream();
xmlDocument.Save(ms);
string text = System.Text.Encoding.GetEncoding("UTF-8").GetString(ms.GetBuffer(), 0, (int)ms.Length);
Output is
<xml version='1.0' encoding='UTF-8'?>
<soapenv:Header xmlns:soapenv=" " />
I was trying to create like this
<xml version='1.0' encoding='UTF-8'?>
<soapenv:Header/>
How do I eliminate xmlns:soapenv=" " from soapenv:Header?
Any help would be greatly appreciated.
What you’re asking how to do is create ill-formed (syntactically incorrect) XML. The XmlDocument API is not designed to do that.
If you use the element name
that means
soapenvis a namespace prefix, which has been declared on this element (or an ancestor) using thexmlns:soapenvpseudoattribute. If it has not been declared, your XML output will not be accepted by any self-respecting XML parser.The method signature you called,
takes two arguments: the qualified name of the element (which may include a prefix, as it does in your case); and a namespace URI. The documentation shows what the expected output is:
The expected namespace URI for this element is “http://schemas.xmlsoap.org/soap/envelope/”. So you will want to declare the soapenv prefix with that namespace URI:
and use it thus: