XmlWriter The ':' character, hexadecimal value 0x3A, cannot be included in a name

StackOverflow https://stackoverflow.com/questions/13680615

  •  04-12-2021
  •  | 
  •  

Domanda

I am using an XmlWriter and getting the following error:

Invalid name character in 'news:news'. The ':' character, hexadecimal value 0x3A, cannot be included in a name.

does anyone have any idea what is causing this? here is my code:

using (XmlWriter writer = XmlWriter.Create("moo.xml"))
            {               

                writer.WriteStartDocument();
                writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
                writer.WriteAttributeString("xmlns", "news", null, "http://www.google.com/schemas/sitemap-news/0.9");


                writer.WriteStartElement("url");
                writer.WriteElementString("loc", "http://www.monkeys.co.uk");

                writer.WriteStartElement("news:news");
                writer.WriteEndElement();

                writer.WriteEndElement();



                writer.WriteEndElement();
                writer.WriteEndDocument();

            }
È stato utile?

Soluzione

You need to use the overload of WriteStartElement that takes two parameters:

writer.WriteStartElement("news", "http://www.google.com/schemas/sitemap-news/0.9");
//               Tag   ----^       ^--- Namespace
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top