需要生成如下的xml,是google的sitemap文件,里面有两个namespace,网上能找到的这样范例实在太少,小女子水平有限,求助各位,如何用C#生成符合下面格式的xml:<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://example.com/sample.html</loc>
<image:image>
<image:loc>http://example.com/image.jpg</image:loc>
</image:image>
</url>
</urlset>难点1:此xml中有两个namespace,一个是xmlns,另一个是xmlns:image,如何生成?
难点2: xml中貌似不能有节点带有“:”符号,如何生成这样的节点?哭啊……痛苦一天了

解决方案 »

  1.   


    using   System.IO; 
    using   System.Xml; public   class   Sample 

        public   static   void   Main() 
        { 
            //   Create   the   XmlDocument. 
            XmlDocument   doc   =   new   XmlDocument(); 
            doc.LoadXml( " <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    <url>
    <loc>http://example.com/sample.html</loc>
    <image:image>
    <image:loc>http://example.com/image.jpg</image:loc>
    </image:image>
    </url>
    </urlset>");         //   Save   the   document   to   a   file. 
            doc.Save( "data.xml "); 
        } 

      

  2.   

    http://www.oracle.com/technology/global/cn/pub/articles/srivastava_namespaces.html楼主看看吧
      

  3.   

    你是什么意思?要加入数据到xml?
      

  4.   

    以后需要添加n个<url>节点到该xml中
      

  5.   

    两个的还真没搞过,我只搞过一个的。我学习了下,写了下述代码,你自己测试下
    XmlDocument MyXML = new XmlDocument();
                MyXML.Load("1.xml");
                XmlNode Node = MyXML.DocumentElement;
                XmlElement URL= MyXML.CreateElement("url");
                XmlElement LOC = MyXML.CreateElement("loc");
                LOC.InnerText = URL;
                XmlElement images = MyXML.CreateElement("image:image");
                images.InnerText = URL;
                XmlElement image = MyXML.CreateElement("image:loc");
                image.InnerText = images;
                URL.AppendChild();//括号里自己加,下同
                LOC.AppendChild();
                image.AppendChild();
                MyXML.Save("1.xml");
      

  6.   

    在工程文件的debug文件夹下新建1.xml,里面最少要求有
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    把上面这句复制到txt文档,然后保存,改后缀为xml,移到debug文件夹下
      

  7.   

    把头写好,直接用loadXML加载就行了。
      

  8.   

    问题解决了,非常感谢各位!
    代码如下:
    XmlDocument sitemapxml = new XmlDocument();
    sitemapxml.LoadXml( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\"></urlset>"); 
    sitemapxml.Save(xmlname);
    XmlNodeList eUrlsets = sitemapxml.GetElementsByTagName("urlset");
    XmlNode eUrlset = eUrlsets[0];
    XmlElement eUrl = sitemapxml.CreateElement("", "url", sitemapxml.DocumentElement.NamespaceURI);
    XmlElement eLoc = sitemapxml.CreateElement("", "loc", sitemapxml.DocumentElement.NamespaceURI);
    eLoc.InnerText = items[0];
    XmlElement eImgImg = sitemapxml.CreateElement("image", "image", "http://www.google.com/schemas/sitemap-image/1.1");
    XmlElement eImgLoc = sitemapxml.CreateElement("image", "loc", "http://www.google.com/schemas/sitemap-image/1.1");
    eImgLoc.InnerText = items[1];
    eImgImg.AppendChild(eImgLoc);
    eUrl.AppendChild(eLoc);
    eUrl.AppendChild(eImgImg);
    eUrlset.AppendChild(eUrl);
    sitemapxml.Save(xmlname);
      

  9.   

    <image:image>这个节点 的意思 是后面的image节点 是在 image 所代表的命名空间下,也就是 http://www.google.com/schemas/sitemap-image/1.1的命名空间