且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用LINQ生成XML时,如何从元素中删除xmlns?

更新时间:2022-11-28 19:46:20

听起来好像您希望Sitemap名称空间中的url元素(以及所有子元素)成为 be ,所以您需要:

It sounds like you want the url element (and all subelements) to be in the sitemap namespace, so you want:

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";

XElement locElement = new XElement(ns + "loc", location);
XElement lastmodElement = new XElement(ns + "lastmod", modifiedDate.ToString("yyyy-MM-dd"));
XElement changefreqElement = new XElement(ns + "changefreq", changeFrequency);

XElement urlElement = new XElement(ns + "url");
urlElement.Add(locElement);
urlElement.Add(lastmodElement);
urlElement.Add(changefreqElement);

或更常规地用于LINQ to XML:

or more conventionally for LINQ to XML:

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";

XElement urlElement = new XElement(ns + "url",
    new XElement(ns + "loc", location);
    new XElement(ns + "lastmod", modifiedDate.ToString("yyyy-MM-dd"),
    new XElement(ns + "changefreq", changeFrequency));