0

我正在使用 XmlWriter 并收到以下错误:

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

有谁知道是什么原因造成的?这是我的代码:

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();

            }
4

1 回答 1

4

您需要使用WriteStartElement带有两个参数的重载:

writer.WriteStartElement("news", "http://www.google.com/schemas/sitemap-news/0.9");
//               Tag   ----^       ^--- Namespace
于 2012-12-03T09:52:50.037 回答