tags:

views:

983

answers:

1

Hi, I want to create an XML file with folllowing header dynamically.

<?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"&gt;
<url>

How should i create this urlset node.

+2  A: 

With 3.5, something like:

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", null),
    new XElement(ns + "urlset",
        new XAttribute(XNamespace.Xmlns + "xsi", xsi),
        new XAttribute(xsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
        new XElement(ns + "url")
    )
);
// save/writeto
string s = doc.ToString();
Marc Gravell