需要生成如下的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中貌似不能有节点带有“:”符号,如何生成这样的节点?哭啊……痛苦一天了
<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中貌似不能有节点带有“:”符号,如何生成这样的节点?哭啊……痛苦一天了
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 ");
}
}
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");
<?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文件夹下
代码如下:
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);