加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在.NET中为XAttribute设置命名空间前缀?

发布时间:2020-12-15 06:28:27 所属栏目:百科 来源:网络整理
导读:所有, 我想创建一个肥皂包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来执行此操作,但是我无法弄清楚如何将soa
所有,
我想创建一个肥皂包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

解决方法

创建’encodingStyle’XAttribute(通过使用ns“encodingStyle”)指定命名空间:
XAttribute encoding = new XAttribute(ns + "encodingStyle","http://www.w3.org/2001/12/soap-encoding");

two-parameter XAttribute constructor将XName作为第一个参数.这可以从字符串隐式构造(如您的问题中的代码),也可以直接通过将字符串“添加”到XNamespace来创建XName(如上所述).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读