如何设置一个命名空间preFIX到XAttribute在.NET? [英] How do I set a namespace prefix to an XAttribute in .NET?

查看:122
本文介绍了如何设置一个命名空间preFIX到XAttribute在.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有, 我想创建一个SOAP信封XML文档如:

All, I want to create a soap envelope xml document eg.

<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的要做到这一点,但我无法弄清楚如何添加肥皂 preFIX到的encodingStyle 属性。

I am using System.Xml.Linq to do this but I cannot figure out how to add the soap prefix to the encodingStyle attribute.

到目前为止,我有这样的:

So far, I have this:

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 添加preFIX到一个元素,我可以使用 XAttribute 添加preFIX到 XAttribute ??!

You use XAttribute to add a prefix to an element, can I use XAttribute to add a prefix to an XAttribute??!

感谢,P

推荐答案

指定命名空间,当您创建了encodingStyle的XAttribute(通过使用 NS +的encodingStyle

Specify the namespace when you create the 'encodingStyle' XAttribute (by using ns + "encodingStyle"):

XAttribute encoding = new XAttribute(ns + "encodingStyle", "http://www.w3.org/2001/12/soap-encoding");

双参数XAttribute构造需要一个的XName 作为第一个参数。这可以是从字符串隐式构造(如code在你的问题),或者直接通过添加一个字符串的XNamespace 来创建一个的XName (如上)。

The two-parameter XAttribute constructor takes an XName as the first argument. This can either be constructed implicitly from a string (as in the code in your question), or directly by "adding" a string to an XNamespace to create an XName (as above).

这篇关于如何设置一个命名空间preFIX到XAttribute在.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆