XmlWriter':'字符,十六进制值0x3A,不能包含在名称中

时间:2012-12-03 09:45:19

标签: c# xml xmlwriter

我正在使用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();

            }

1 个答案:

答案 0 :(得分:4)

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

writer.WriteStartElement("news", "http://www.google.com/schemas/sitemap-news/0.9");
//               Tag   ----^       ^--- Namespace