0

So as the title says, I'm trying to write multiple attributes to one tag however, I keep running into errors:

writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
writer.WriteAttributeString("xsi", "schemaLocation", null, "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
writer.WriteAttributeString("xmlns", "xsi", "http://www.sitemaps.org/schemas/sitemap/0.9", "http://www.w3.org/2001/XMLSchema-instance");

Is what I'm using.

The first line gives this error:

System.Xml.XmlException: The prefix '' cannot be redefined from '' to 'http://www.sitemaps.org/schemas/sitemap/0.9' within the same start element tag.

And if you remove it the third line gives this:

System.ArgumentException: Prefix "xmlns" is reserved for use by XML.

Anyone got any ideas? I see no reason for this happening.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
  • I think there was a special way to add namespaces... As far as I remember it was something like using some `NamespaceManager` and stuff.. Google it... – Fᴀʀʜᴀɴ Aɴᴀᴍ May 23 '16 at 12:27
  • You should pass your default namespace to the WriteStartElement method. See [C# XML - Multiple Namespace Declaration with XML Writer](https://stackoverflow.com/questions/7901056/c-sharp-xml-multiple-namespace-declaration-with-xml-writer). – dbc May 23 '16 at 22:48

1 Answers1

1

I see no reason for this happening

Well I do. The first error tells you that you cannot redefine the xmlns namespace because it's the attribute tag for your root element that specifies the xml namespace.

Xml namespaces get resolved (kind of) automatically, I'm guessing this is what you are trying to do:

using (FileStream stream = new FileStream(@"C:\Temp\test.xml", FileMode.Create))
{
    XmlWriter x = new XmlTextWriter(stream, Encoding.UTF8);

    x.WriteStartElement("Root");
    x.WriteAttributeString("xmlns", "xsi", null, "http://www.sitemaps.org/schemas/sitemap/0.9");

    x.WriteStartElement("Element");
    x.WriteAttributeString("xsi", "schemaLocation", null, "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
    x.WriteEndElement();

    x.WriteFullEndElement();
    x.Flush();
}

Result:

<Root xmlns:xsi="http://www.sitemaps.org/schemas/sitemap/0.9">
    <Element xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" />
</Root>

Further thoughts

If you need "http://www.w3.org/2001/XMLSchema-instance" to show up as xmlns add the namespace to the WriteStartElement method, that will give you:

<Root xmlns:xsi="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns="http://www.w3.org/2001/XMLSchema-instance">

If you need an xml declaration, create the XmlWriter like this instead:

XmlWriterSettings settings = new XmlWriterSettings();
settings.NewLineHandling = NewLineHandling.None;
Settings.OmitXmlDeclaration = false;
XmlWriter x = XmlWriter.Create(stream, settings);

Which will result in:

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

at the start of your xml

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62