【问题标题】:how to add xml namespaces如何添加xml命名空间
【发布时间】:2010-10-30 00:17:30
【问题描述】:

这个提要(它的片段)需要看起来完全像这样:

<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">

我要在这个 C# 代码中添加什么来添加额外的 xmlns、xsi 垃圾:

writer.WriteStartDocument();
writer.WriteStartElement("AmazonEnvelope");

如果没有它,此提要将被拒绝--

【问题讨论】:

  • 您没有提到您使用的是哪种编程语言。一个假设是 C#,因为 WriteStartDocument 是 XmlWriter 上的一种方法,但这不是保证。
  • 它在 XML 片段正下方的行中间显示“C#”.....

标签: c# xml api namespaces


【解决方案1】:

试试这个:

writer.WriteStartElement("AmazonEnvelope");
writer.WriteAttributeString(
  "xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString(
  "xsi", "noNamespaceSchemaLocation", null, "amzn-envelope.xsd");
...
writer.WriteEndElement();

【讨论】:

  • 哇,我在这里学到了很多!我现在正在前进......亚马逊接受了提要......敏感的api!
【解决方案2】:

.NET 3.5 是一种选择吗?

XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";

string s = new XElement("AmazonEnvelope",
    new XAttribute(XNamespace.Xmlns + "xsi", ns),
    new XAttribute(ns + "noNamespaceSchemaLocation", "amzn-envelope.xsd")
).ToString();

XmlWriter:

const string ns = "http://www.w3.org/2001/XMLSchema-instance";
writer.WriteStartDocument();
writer.WriteStartElement("AmazonEnvelope");
writer.WriteAttributeString("xmlns", "xsi", "", ns);
writer.WriteAttributeString("xsi", "noNamespaceSchemaLocation",
      ns, "mzn-envelope.xsd");
writer.WriteEndDocument();

【讨论】:

    猜你喜欢
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多