【问题标题】:XMLWriter syntax problemsXMLWriter 语法问题
【发布时间】:2010-03-09 08:38:49
【问题描述】:

我正在尝试创建一个允许用户输入信息的 asp.net Web 表单,然后通过 XMLwriter 将此信息发送到 Web 服务。

这是应该输出的xml的sn-p;

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body xmlns:ns1="http://its/foo.wsdl">

我尝试通过代码来操作它;

xml.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")     
xml.WriteAttributeString("xmlns", "ns1", "http://its/foo.wsdl")

但我收到此错误:

The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'.

谁能告诉我我做错了什么?

谢谢。

【问题讨论】:

    标签: asp.net xml web-services syntax


    【解决方案1】:
    using (var writer = XmlWriter.Create(Console.Out))
    {
        writer.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
        writer.WriteStartElement("soap", "Body", null);
        writer.WriteAttributeString("xmlns", "ns1", null, "http://its/foo.wsdl");
        // ... add other tags
        writer.WriteEndElement();
        writer.WriteEndElement();
    }
    

    【讨论】:

    • @Darin,我认为他不需要明确创建xmlns:ns1
    • 谢谢,使用这段代码我得到了空值错误,说使用 system.dbnull 而不是这也会导致错误
    • 约翰,你能给我更多关于为什么我不需要显式创建 xmlns:ns1 的信息吗?以及上面的xml如何在没有please的情况下生成。
    • @Phil,我提供的 sn-p 生成的正是您所期望的 XML。
    • 谢谢达林,我有点被 C# 抛出了:0) 。现在工作。
    【解决方案2】:

    简而言之,“xmlns”“属性”不是真正的属性。它们是命名空间声明。您不需要生成它们。它们将根据需要生成,作为将内容生成到不同 XML 命名空间的一部分。

    【讨论】:

    • 谢谢,但我需要做什么?
    • 忽略它们,做其他事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    相关资源
    最近更新 更多