XML 空白属性创建,出现 ':' 问题特点
我正在尝试使用以下代码创建 xml。
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);
输出是
<xml version='1.0' encoding='UTF-8'?>
<soapenv:Header xmlns:soapenv=" " />
我试图像这样创建
<xml version='1.0' encoding='UTF-8'?>
<soapenv:Header/>
如何从 soapenv:Header
中消除 xmlns:soapenv=" "
?
任何帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您要问的是如何创建格式不正确(语法不正确)的 XML。 XmlDocument API 并不是为此而设计的。
如果您使用的元素名称
意味着
soapenv
是命名空间前缀,它已使用xmlns:soapenv
伪属性在此元素(或祖先)上声明。如果没有声明,您的 XML 输出将不会被任何有自尊的 XML 解析器接受。您调用的方法签名
采用两个参数:元素的限定名称(可能包括前缀,就像您的情况一样);和命名空间 URI。 文档显示了预期的输出是:
此元素的预期命名空间 URI 是“http://schemas.xmlsoap.org/soap/envelope/”。因此,您需要使用该命名空间 URI: 声明soapenv 前缀,
并这样使用它:
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
soapenv
is a namespace prefix, which has been declared on this element (or an ancestor) using thexmlns:soapenv
pseudoattribute. 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:
在没有命名空间的情况下创建(例如使用默认命名空间),那么该部分就不再是必需的:
Create without the namespace (e.g. use default namespace) and then that part won't be necessary: