您好我正在尝试编写一个字符串:
<xhtml:link rel="alternate" hreflang="de" href="http://www.example.com/de" />
使用XmlTextWriter
类
我尝试过这段代码:
// Write Alternative links
_writer.WriteStartElement("xhtml:link");
_writer.WriteAttributeString("rel","alternate");
_writer.WriteAttributeString("hreflang", "de");
_writer.WriteAttributeString("href", "http://example.com/de");
_writer.WriteEndElement();
哪会产生此错误: 链接上的命名空间前缀xhtml未定义
但我不需要为xhtml:link
问题:如何使用XmlTextWriter实现我需要的字符串?
但是现在我还有另一个问题......首先我会展示代码:
private readonly XNamespace nsXhtml = "http://www.w3.org/1999/xhtml";
private readonly XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
private readonly XNamespace nsXsi = "http://www.w3.org/2001/XMLSchema-instance";
private readonly XNamespace nsLocation = "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd";
public XDocument Generate()
{
var sitemap = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
var urlSet = new XElement(nsSitemap + "urlset",
new XAttribute("xmlns", nsSitemap),
new XAttribute(XNamespace.Xmlns + "xhtml", nsXhtml),
new XAttribute(XNamespace.Xmlns + "xsi", nsXsi),
new XAttribute(nsXsi + "schemaLocation", nsLocation),
from node in GenerateUrlNodes() // Provides a collection of "objects", actually it doesn't matter since we anyway convert them to XElement below...
select WriteUrlLocation(node.Url,node.UpdateFrequency,node.LastModified));
sitemap.Add(urlSet);
return sitemap;
}
protected XElement WriteUrlLocation(string url, UpdateFrequency updateFrequency, DateTime lastUpdated)
{
var urlNode = new XElement(nsSitemap + "url",
new XElement(nsSitemap + "loc", url),
new XElement(nsSitemap + "changefreq", updateFrequency),
new XElement(nsSitemap + "lastmod", lastUpdated)
);
var linkNode = new XElement(nsXhtml + "link",
new XAttribute("rel", "alternate"),
new XAttribute("hreflang", "de"),
new XAttribute("href", "http://example.com/de"));
urlNode.Add(linkNode);
return urlNode;
}
问题是当我在Controller检查生成的站点地图时:
public ActionResult Sitemap()
{
var sitemap = _sitemapGenerator.Generate().ToString();
return Content(sitemap,"text/xml");
}
整个xml不是预期的,<xhtml:link>
元素使用非空的结束标记呈现(因此我不知道这是否是一个问题)..请看图像< / p>
答案 0 :(得分:3)
您应该更改为使用XmlWriter.StartElement
的其他重载。例如:
_writer.WriteStartElement("link", "http://www.w3.org/1999/xhtml");
假设您已为命名空间xhtml
获得http://www.w3.org/1999/xhtml
的前缀别名。我仍然建议尽快转换为使用LINQ to XML ... XmlWriter
非常适合您真正需要流式传输数据的情况(例如,当它巨大时)但除此之外,LINQ to XML使事情变得更容易:
XNamespace xhtml = "http://www.w3.org/1999/xhtml";
var element = new XElement(xhtml + "link",
new XAttribute("rel", "alternate"),
new XAttribute("hreflang", "de"),
new XAttribute("href", "http://example.com/de"));
parent.Add(element);
答案 1 :(得分:0)
如果您将使用XML Writer并编写此
<xhtml:link rel="alternate" hreflang="en" href="www.yoursite.com" />
您可以为.NET CORE 2.0选择此代码:
foreach (SitemapNodeAlternate a in alternate)
{
MyWriter.WriteStartElement("xhtml", "link", null);
MyWriter.WriteAttributeString("rel", "alternate");
MyWriter.WriteAttributeString("href", a.href);
MyWriter.WriteAttributeString("hreflang", a.hreflang);
MyWriter.WriteEndElement();
}