Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加带有前缀但不带命名空间的起始元素_C#_Xml_Xml Namespaces_Prefix_Xmlwriter - Fatal编程技术网

C# 添加带有前缀但不带命名空间的起始元素

C# 添加带有前缀但不带命名空间的起始元素,c#,xml,xml-namespaces,prefix,xmlwriter,C#,Xml,Xml Namespaces,Prefix,Xmlwriter,有没有办法在XmlWriter中使用WriteStarteElement函数,如下所示: XmlWriter.WriteStartElement("prefix", "name", null); 发生错误:System.ArgumentException:'无法将前缀用于 空命名空间。' 我不想在创建元素时设置名称空间URI。 稍后,我将在创建其他属性时通过WriteAttributeString()添加它。否,未绑定到命名空间URI的命名空间前缀没有意义,在XML文档中是不允许的 我不想在创

有没有办法在XmlWriter中使用WriteStarteElement函数,如下所示:

XmlWriter.WriteStartElement("prefix", "name", null);
发生错误:System.ArgumentException:'无法将前缀用于 空命名空间。'

我不想在创建元素时设置名称空间URI。

稍后,我将在创建其他属性时通过WriteAttributeString()添加它。

否,未绑定到命名空间URI的命名空间前缀没有意义,在XML文档中是不允许的

我不想在创建元素时设置名称空间URI。稍后,我将在创建其他属性时通过WriteAttributeString()添加它

前缀始终属于命名空间。通过定义非空名称空间,将自动创建
xmlns
属性:

writer.WriteStartElement("prefix", "localName", "ns"); // <prefix:localName xmlns:prefix="ns" />
writer.writeStarteElement(“前缀”、“本地名称”、“ns”);//

我也遇到了同样的问题,我正在寻找解决方案,最终,我意识到我应该使用我已经定义的名称空间

我有这样的东西来创建元素:

xmlWriter.WriteStartElement("xhtml", "link", "xmlns");
结果是:

<?xml version="1.0" encoding="utf-8" ?>
<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://www.my-site.com/en/home</loc>
        <xhtml:link rel="alternative" hreflang="en" href="https://www.my-site.com/en/home" xmlns:xhtml="xmlns" />
        <xhtml:link rel="alternative" hreflang="ar" href="https://www.my-site.com/fr/home" xmlns:xhtml="xmlns" />
        <xhtml:link rel="alternative" hreflang="fa" href="https://www.my-site.com/fa/home" xmlns:xhtml="xmlns" />
    </url>
</urlset>
所以我把
http://www.w3.org/1999/xhtml
改为命名空间或ns,如下所示:

xmlWriter.WriteStartElement("xhtml", "link", "http://www.w3.org/1999/xhtml");
现在它正是我所需要的:

<?xml version="1.0" encoding="utf-8" ?>
<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://www.my-site.com/en/home</loc>
        <xhtml:link rel="alternative" hreflang="en" href="https://www.my-site.com/en/home" />
        <xhtml:link rel="alternative" hreflang="ar" href="https://www.my-site.com/fr/home" />
        <xhtml:link rel="alternative" hreflang="fa" href="https://www.my-site.com/fa/home" />
    </url>
</urlset>

https://www.my-site.com/en/home

writeStarteElement的参数让您能够克服困惑,这很好,但您的问题不同:OP的问题在于想要创建一个名称空间前缀未定义的元素,这在名称空间格式良好的XML中是不允许的;您的问题是,您只是误解了
writeStarteElement()
的第三个参数的用途,但在更正错误后,您同意将名称空间前缀绑定到名称空间。
<?xml version="1.0" encoding="utf-8" ?>
<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://www.my-site.com/en/home</loc>
        <xhtml:link rel="alternative" hreflang="en" href="https://www.my-site.com/en/home" />
        <xhtml:link rel="alternative" hreflang="ar" href="https://www.my-site.com/fr/home" />
        <xhtml:link rel="alternative" hreflang="fa" href="https://www.my-site.com/fa/home" />
    </url>
</urlset>