c# - XElement 中的 XML 解析 ':'

标签 c# xml windows-phone-7 namespaces xmpp

我正在创建名为“stream:stream”的 XElement 对象,但它引发了 XMLException 这样 ':' 不能包含在名称中。

这里的第一个流是一个命名空间。

最佳答案

您可以像这样指定命名空间:

XNamespace streamNs = "some-url-here";
// The + operator here creates an XName
XElement element = new XElement(streamNs + "stream");

要创建一个“stream:stream”元素,您需要在某些元素中为 stream 添加一个 xmlns 属性,例如

// Add this to an element - either the element in the namespace, or some parent
// element. The document root is a common place to put all namespaces...
XAttribute streamNs = new XAttribute(XNamespace.Xmlns + "stream",
                                     streamNs.NamespaceName);

关于c# - XElement 中的 XML 解析 ':',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8712667/

相关文章:

c# - 将图像源设置为 URI

C# 正则表达式总是返回 FALSE

c# - 如何正确关闭或销毁TCP客户端?

c# - 可查询属性不明确引用错误

c# - 检查两个可枚举之间是否存在共同的单个元素的最快方法

Android-ListView的最后一项不可见

xml - 用于查找标签名称包含 'Name' 的元素的 XPath 表达式

silverlight - 如何在按住另一根手指时记录触摸

javascript - xmldoc.selectNodes() 无法正常工作

c# - 在过渡期间禁用 Pivot 中的输入?