c# - 如何从 C# 中的对象生成 xml 并格式化它?

标签 c# xml linq linq-to-xml

在这里,我尝试从列表中格式化 XML,但没有得到正确的格式。这是我的代码:

protected void GenerateXml(string url, List<string> listitems)  //generateXml
{
    XNamespace nsXhtml = "http://www.w3.org/1999/xhtml";
    XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
    XNamespace nsImage = "http://www.google.com/schemas/sitemap-image/1.1";
    var sitemap = new XDocument(new XDeclaration("1.0", "UTF-8", ""));

    var urlSet =
        new XElement(
            nsSitemap + "urlset",
            new XAttribute("xmlns", nsSitemap),
            new XAttribute(XNamespace.Xmlns + "image", nsXhtml),
            from urlNode in listitems
            select 
                new XElement(
                    nsSitemap + "url",
                    new XElement(nsSitemap + "loc", url),
                    new XElement(nsSitemap + "image",
                    new XElement(nsSitemap + "imageloc", urlNode))));

    sitemap.Add(urlSet);
    sitemap.Save(System.Web.HttpContext.Current.Server.MapPath("/Static/sitemaps/Sitemap-image.xml"));
}

...并获取如下格式:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.w3.org/1999/xhtml">
  <url>
    <loc>http://example.com/intl/cars/new-models/the-new-s90</loc>
    <image> 
      <imageloc>http://example.com/static/images/volvo-logo-scaled.png</imageloc>
    </image>
  </url>
  <url>
    <loc>http://example.com/intl/cars/new-models/the-new-s90</loc>
    <image>
      <imageloc>http://assets.example.com/intl/~/media/images/galleries/new-cars/packshots/small/allnew_xc90-side_2.png</imageloc>
    </image>
  </url>
</urlset>

但我需要这种格式:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
  <url>
    <loc>http://example.com/sample.html</loc>
    <image:image>
      <image:loc>http://example.com/image.jpg</image:loc>
    </image:image>
    <image:image>
      <image:loc>http://example.com/photo.jpg</image:loc>
    </image:image>
  </url> 
</urlset> 

有什么建议吗?

最佳答案

除了正确处理多个嵌套元素之外,您从未将 image 前缀分配给应该拥有它们的元素,您继续使用全局命名空间:

new XElement(nsSitemap + "image",
   new XElement(nsSitemap + "imageloc", urlNode)

nsSitemap 应为 nsImage,“imageloc”应为“loc”。

对代码进行一些小调整即可获得所需的内容:

protected void GenerateXml(string url, List<string> listitems)  //generateXml
{

    XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
    XNamespace nsImage = "http://www.google.com/schemas/sitemap-image/1.1";

    var sitemap = new XDocument(new XDeclaration("1.0", "UTF-8", ""));

    var urlSet = new XElement(nsSitemap + "urlset",
        new XAttribute("xmlns", nsSitemap),
        new XAttribute(XNamespace.Xmlns + "image", nsImage),
        new XElement(nsSitemap + "url",
        new XElement(nsSitemap + "loc", url),
        from urlNode in listitems
        select new XElement(nsImage + "image",
               new XElement(nsImage + "loc", urlNode)
           )));
        sitemap.Add(urlSet);          sitemap.Save(System.Web.HttpContext.Current.Server.MapPath("/Static/sitemaps/Sitemap-image.xml"));
}

请注意以下更改:

new XAttribute(XNamespace.Xmlns + "image", nsImage);

这会正确设置命名空间以符合您的预期输出。

new XElement(nsImage + "image",
new XElement(nsImage + "loc", urlNode)

这会正确设置image前缀。

请注意“loc”和“url”如何移至 from 查询之前。

上面的代码产生以下输出 XML:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
  <url>
    <loc>http://example.com/sample.html</loc>
    <image:image>
      <image:loc>http://example.com/image.jpg</image:loc>
    </image:image>
    <image:image>
      <image:loc>http://example.com/photo.jpg</image:loc>
    </image:image>
  </url>
</urlset>

关于c# - 如何从 C# 中的对象生成 xml 并格式化它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34657515/

相关文章:

c# - 如何读取命名空间 c# LINQ to XML

linq - 带 Take 和不带 Take 的 linq 查询有什么区别?

c# - 拆分作为对象属性的字符串,从拆分的字符串创建新对象。有优雅的方法吗?

c# - LINQ to SQL - 为什么不能在 ORDER BY 之后使用 WHERE?

c# - EWS : Exchange Web Service. 多次调用 ResolveName - 性能命中(当然)

c# - 在 WPF 应用程序中嵌入 Youtube 视频

C# 代码自动授予 IIS 对 Windows Server 2008 上的文件夹的写入权限?当前抛出异常

javascript - 服务器标记对于 ItemTemplate 的 ASP DropDownList 格式不正确

php - Magento 更新 CMS 页面 XML 以添加 block - block 未加载

c# LINQ-to-XML 在排序后更新(保存)文件中的元素列表