tags:

views:

35

answers:

1

Hi,

Below is the XML schema I am trying to conform to:

<?xml version="1.0" encoding="UTF-8"?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"&gt;

   <url>

      <loc>http://www.example.com/&lt;/loc&gt;

      <lastmod>2005-01-01</lastmod>

      <changefreq>monthly</changefreq>

      <priority>0.8</priority>

   </url>

</urlset>

Below is what I have managed to produce:

<?xml version="1.0" encoding="utf-8"?>
<urlset>
  <url>
    <loc>http://www.ign.com&lt;/loc&gt;
    <lastmod>2005-01-01</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
</urlset>

As you can see there is one main difference, I can't seem to be able to re-create the attribute string of the 'urlset' element.

Below is the code I am using:

            // Create the settings object that will define the settings that our writer will use.
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Encoding = Encoding.UTF8;
        settings.Indent = true;
        settings.OmitXmlDeclaration = false;

        // create the XML writer object.
        XmlWriter xmlW = XmlWriter.Create("SiteMap.xml", settings);

        // write the start header for the XML document.
        xmlW.WriteStartDocument();

        xmlW.WriteStartElement("urlset");

        xmlW.WriteStartElement("url");

        xmlW.WriteElementString("loc", "http://www.ign.com");
        xmlW.WriteElementString("lastmod", "2005-01-01");
        xmlW.WriteElementString("changefreq", "monthly");
        xmlW.WriteElementString("priority", "0.8");

        xmlW.WriteEndElement();

        xmlW.WriteFullEndElement();

        // close the writer.
        xmlW.Close();

        Console.ReadKey();

My code is simple enough and it still doesn't match the sample xml I was given to conform to. Can you guys help make sure that both sets of xml match each other?

+5  A: 

You came very close:

xmlW.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");

However, you may find it a lot easier to use either serialization or LINQ-to-XML.

Marc Gravell
darn, you beat me to it :)
Richard J. Ross III
darn it .... is that how close I was. Thanks man. Appreciate it mate, but i got pulled of main project for this one, so I ont want to go out of my way too much. Thanks tho.
IbrarMumtaz