C#:显式命名空间未出现在生成的 XML 中
给定以下 C# 代码来生成 XML 文件:
XmlDocument requestXML = new XmlDocument();
XmlDeclaration declaration = requestXML.CreateXmlDeclaration( "1.0", "utf-8", null );
requestXML.AppendChild( declaration );
XmlElement soapEnvelope = requestXML.CreateElement( "soap:Envelope" );
soapEnvelope.SetAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
soapEnvelope.SetAttribute( "xmlns:xsd", "http://www.w3.org/2001/XMLSchema" );
soapEnvelope.SetAttribute( "xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/" );
我在 requestXML.OuterXML
中看到的 XML 显示
<Envelope ...>
与
<soap:Envelope ...>
我预期的不同。我做错了什么?
Given the following C# code to generate an XML file:
XmlDocument requestXML = new XmlDocument();
XmlDeclaration declaration = requestXML.CreateXmlDeclaration( "1.0", "utf-8", null );
requestXML.AppendChild( declaration );
XmlElement soapEnvelope = requestXML.CreateElement( "soap:Envelope" );
soapEnvelope.SetAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
soapEnvelope.SetAttribute( "xmlns:xsd", "http://www.w3.org/2001/XMLSchema" );
soapEnvelope.SetAttribute( "xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/" );
The XML I'm seeing in requestXML.OuterXML
shows
<Envelope ...>
Rather than
<soap:Envelope ...>
as I would expect. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您可以尝试使用命名空间 uri 作为参数 #2 的
CreateElement
重载。Maybe you could try the
CreateElement
overload that takes a namespace uri as parameter #2.