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

2023-12-29

全部, 我想创建一个肥皂信封 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>

You use XAttribute要向元素添加前缀,我可以使用XAttribute添加前缀XAttribute??!

谢谢,P


创建“encodingStyle”XAttribute 时指定命名空间(通过使用ns + "encodingStyle"):

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

The 二参数 XAttribute 构造函数 http://msdn.microsoft.com/en-us/library/bb336630.aspx需要一个XName作为第一个参数。这可以从隐式构造string(如您问题中的代码),或直接“添加”string to an XNamespace创建一个XName(如上)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 .NET 中为 XAttribute 设置命名空间前缀? 的相关文章

随机推荐