我有一个图像站点地图,其中包含如下节点:
<image:image xmlns:image="image">
<image:loc>
https://website
</image:loc>
<image:caption/>
<image:title>logo72x72</image:title>
</image:image>
这是创建它的代码:
XmlNode imageNode = doc.CreateElement("image", "image", "image");
XmlNode imageLocNode = doc.CreateElement("image", "loc", "image");
imageLocNode.AppendChild(doc.CreateTextNode(imageUrl));
imageNode.AppendChild(imageLocNode);
XmlNode imageCaptionNode = doc.CreateElement("image", "caption", "image");
imageCaptionNode.AppendChild(doc.CreateTextNode(imageCaption));
imageNode.AppendChild(imageCaptionNode);
XmlNode imageTitleNode = doc.CreateElement("image", "title", "image");
imageTitleNode.AppendChild(doc.CreateTextNode(imageTitle));
imageNode.AppendChild(imageTitleNode);
urlNode.AppendChild(imageNode);
但是,在 Google Search Console 中,由于图像标记与 Google 的文档不匹配,我收到了错误。根据 Google 的说法,图像标签应类似于 <image:image>
,不是<image:image xmlns:image="image">
.
我将代码更改为 doc.CreateElement("image", "image")
但这将输出更改为 <image xmlns:image="image">
(它删除了我想要的部分,并留下了 xmlnls)。如何防止添加 xmlns 属性?
编辑:命名空间在我的文档的根目录中声明:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
最佳答案
您需要为每个元素使用正确的命名空间。命名空间是 http://www.google.com/schemas/sitemap-image/1.1
,而不是 image
。
doc.CreateElement("image", "image", "http://www.google.com/schemas/sitemap-image/1.1")`
image
是命名空间前缀,命名空间前缀必须在元素中通过xmlns:image="..."
声明或任何祖先元素。在您的情况下,它是在文档的根目录中声明的,因此不会再次添加。
关于c# - 如何创建没有 xmnls 属性的 XML 节点(例如 "image:image")? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37733581/