我有一个xml文件。我在c#中以String列表的形式有很多url。我希望通过迭代将爬网URL中的这些URL放在loc的xml子标记中。请帮帮我。
List<string> crawledUrls = new List<string>(); // List of included URLS in c#
public void xmlfile()
{
XmlTextWriter writer = new XmlTextWriter("product.xml", System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.WriteComment(Environment.NewLine + "sitemap-generator-url=http://www.auditmypc.com/free-sitemap-generator.asp" + Environment.NewLine);
writer.WriteComment(Environment.NewLine + " This sitemap was created using the free tool found here: http://www.auditmypc.com/free-sitemap-generator.asp" + Environment.NewLine);
writer.WriteComment(Environment.NewLine + "Audit My PC also offers free security tools to help keep you safe during internet travels");
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement("urlset");
createNode("1", writer);
createNode("2", writer);
createNode("3", writer);
createNode("4", writer);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
private void createNode(string link, XmlTextWriter writer)
{
writer.WriteStartElement("url");
writer.WriteStartElement("loc");
writer.WriteString(link);
writer.WriteEndElement();
writer.WriteEndElement();
}
预期产出:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<!-- sitemap-generator-url=http://www.auditmypc.com/free-sitemap-generator.asp -->
<!-- This sitemap was created using the free tool found here: http://www.auditmypc.com/free-sitemap-generator.asp -->
<!-- Audit My PC also offers free security tools to help keep you safe during internet travels -->
-<urlset> -<url> <loc>http://www.friferie.dk/inspiration/India</loc> </url> -<url> <loc>http://www.friferie.dk/inspiration/Germany</loc> </url> -<url> <loc>http://www.friferie.dk/inspiration/Danmark</loc> </url> -<url> <loc>http://www.friferie.dk/inspiration/Spain</loc> </url> </urlset>
答案 0 :(得分:0)
要使用迭代,您可以执行类似的操作
static void Main(string[] args)
{
List<string> crawledUrls = new List<string>(); // List of included URLS in c#
crawledUrls.Add("http://www.friferie.dk/inspiration/India");
crawledUrls.Add("http://www.friferie.dk/inspiration/UK");
string comment = @"sitemap-generator-url=http://www.auditmypc.com/free-sitemap-generator.asp" + Environment.NewLine + Environment.NewLine+
"This sitemap was created using the free tool found here: http://www.auditmypc.com/free-sitemap-generator.asp" + Environment.NewLine + Environment.NewLine+
"Audit My PC also offers free security tools to help keep you safe during internet travels";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XElement root = new XElement("urlset",
new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"),
new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
new XComment(comment));
List<XElement> ChildNodes = new List<XElement>();
foreach(string url in crawledUrls)
{
ChildNodes.Add(CreateXMLNode(url));
}
root.Add(ChildNodes);
root.Save("d:/product.xml");
}
public static XElement CreateXMLNode(string url)
{
XElement urlnode = new XElement("url");
urlnode.Add(new XElement("loc",url));
return urlnode;
}