2010-07-27 14 views
6

Tôi đang tạo ra một sitemap.xml độngCách tạo thuộc tính xsi: schemalocation một cách chính xác khi tạo một sitemap.xml động với LINQ to XML?

Theo sitemaps.org đây là tiêu đề cho một sitemap.xml

<?xml version='1.0' encoding='UTF-8'?> 
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" 
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 
    <url> 
    ... 
    </url> 
</urlset> 

Vì vậy, tôi đang sử dụng LINQ to XML để tạo ra sitemap.xml

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; 
return new XElement(ns + "urlset", 
    new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"), 
    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"), 
    //new XAttribute("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"), 
    from node in new GetNodes() 
    select new XElement(ns + "url", 
     new XElement(ns + "loc", node.Loc), 
     new XElement(ns + "lastmod", node.LastMod), 
     new XElement(ns + "priority", node.Priority) 
    ) 
).ToString(); 

Dòng nhận xét là dòng tôi không thể nhận được đúng.
Làm cách nào tôi có thể đặt thuộc tính "xsi: schemalocation"?

Cảm ơn.

Trả lời

4

Ok, tôi hiểu đúng. Nhờ Mike Caron
Nếu tôi khai báo XAtrribute (XNamespace.Xmlns + "xsi", ...) sau đó nó hoạt động

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; 
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; 
return new XElement(ns + "urlset", 
    new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"), 
    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"), 
    new XAttribute(xsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"), 
    from node in GetNodes() 
    select new XElement(ns + "url", 
     new XElement(ns + "loc", node.Loc), 
     new XElement(ns + "lastmod", node.LastMod), 
     new XElement(ns + "priority", node.Priority) 
    ) 
).ToString(); 
+0

Để công bằng, nó không quan trọng một chút những gì các identifier không gian tên thực tế là. Nó có thể là "carlosmunoz" cho tất cả những gì quan trọng :) –

+0

Cool, sau đó tôi sẽ sử dụng "carlosmunoz" –

+0

Hey Carlos, phương thức 'GetNodes' trở lại là gì? Cảm ơn trước. – Ethan

3

Tôi không biết LINQ to XML, nhưng sau khi xem nhanh tài liệu, hãy thử này:

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; 
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; 
return new XElement(ns + "urlset", 
    new XAttribute(xsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"), 
    from node in new GetNodes() 
    select new XElement(ns + "url", 
     new XElement(ns + "loc", node.Loc), 
     new XElement(ns + "lastmod", node.LastMod), 
     new XElement(ns + "priority", node.Priority) 
    ) 
).ToString(); 

Lưu ý rằng tôi không thiết lập các thuộc tính xmlns một cách rõ ràng. Tôi nghi ngờ chúng được tạo tự động. Ngoài ra, emptor caveat, vì điều này không được thử nghiệm.

+0

này hầu hết các công trình nhưng nó tạo ra: p1 thay vì: xsi kể từ chuỗi "xsi "không được đặt ở bất kỳ đâu. –

Các vấn đề liên quan