【问题标题】:XML parsing ':' in XElementXElement 中的 XML 解析“:”
【发布时间】:2012-02-01 12:23:30
【问题描述】:

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

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

【问题讨论】:

  • 通过发布一些代码来展示您已经尝试过的内容是个好主意。您可能需要创建包含命名空间的 XName。 (字符串被隐式转换为 XNames)
  • 嗨拉克什。我对如何创建 xmpp 聊天客户端有疑问。这是我的帖子stackoverflow.com/questions/9781975/… 当您在 xmpp 聊天应用程序中越过初级阶段时,您的回答对我有很大帮助。

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


【解决方案1】:

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

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);

【讨论】:

    【解决方案2】:

    在开始在元素中使用命名空间之前,您可能必须确保将命名空间添加到文档中:
    http://msdn.microsoft.com/en-us/library/bb387075.aspx

    【讨论】:

      猜你喜欢
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多