总之,我想创建一个soap信封xml文档,例如。
<soap:Envelope soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding" xmlns:soap="http://www.w3.org/2001/12/soap-envelope"></soap:Envelope>我使用System.Xml.Linq来完成此操作,但是我不知道如何将soap前缀添加到encodingStyle属性中。
到目前为止,我有这样的想法:
XNamespace ns = XNamespace.Get("http://www.w3.org/2001/12/soap-envelope");
XAttribute prefix = new XAttribute(XNamespace.Xmlns + "soap", ns);
XAttribute encoding = new XAttribute("encodingStyle", "http://www.w3.org/2001/12/soap-encoding");
XElement envelope = new XElement(ns + "Envelope", prefix, encoding);这给了我
<soap:Envelope encodingStyle="http://www.w3.org/2001/12/soap-encoding" xmlns:soap="http://www.w3.org/2001/12/soap-envelope"></soap:Envelope>您使用XAttribute为元素添加前缀,我可以使用XAttribute为XAttribute添加前缀吗??!
谢谢,P
发布于 2010-12-10 14:39:18
在创建'encodingStyle‘XAttribute时指定命名空间(通过使用ns + "encodingStyle"):
XAttribute encoding = new XAttribute(ns + "encodingStyle", "http://www.w3.org/2001/12/soap-encoding");two-parameter XAttribute constructor将XName作为第一个参数。这可以从string隐式构造(如问题中的代码所示),或者直接通过将string添加到XNamespace来创建XName (如上所述)。
发布于 2010-12-10 14:42:16
您需要将XAttribute的XName与XNamespace结合使用。我知道的没错。不管怎么说,试试这个。
XNamespace soap = "http://www.w3.org/2001/12/soap-envelope";
XAttribute encoding = new XAttribute(soap + "encodingStyle",
"http://www.w3.org/2001/12/soap-encoding");发布于 2020-03-06 21:18:17
您需要使用XName.Get方法来构造带有命名空间的属性名称:
var xName = XName.Get("myAttributeName", "http://www.w3.org/2001/XMLSchema-instance");
var attr = new XAttribute(xName, "myAttributeValue");https://stackoverflow.com/questions/4406330
复制相似问题