C# 如何使用linq格式化xml?
在这里,我使用linq创建一个xml,而不是使用所需的格式C# 如何使用linq格式化xml?,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,在这里,我使用linq创建一个xml,而不是使用所需的格式 List<string> listvalue = new List<string>(); listvalue.Add("http://example.com/sample.html"); listvalue.Add("http://example.com/new.html"); foreach (string url in listvalue) { var document = new HtmlWeb()
List<string> listvalue = new List<string>();
listvalue.Add("http://example.com/sample.html");
listvalue.Add("http://example.com/new.html");
foreach (string url in listvalue)
{
var document = new HtmlWeb().Load(url);
var urls = document.DocumentNode.Descendants("img")
.Select(e => e.GetAttributeValue("src", null))
.Where(s => !String.IsNullOrEmpty(s));
List<string> asList = urls.ToList();
GenerateXml(url, asList);
}
List listvalue=new List();
listvalue.Add(“http://example.com/sample.html");
listvalue.Add(“http://example.com/new.html");
foreach(listvalue中的字符串url)
{
var document=new HtmlWeb().Load(url);
var url=document.DocumentNode.substands(“img”)
.Select(e=>e.GetAttributeValue(“src”,null))
.Where(s=>!String.IsNullOrEmpty(s));
List asList=url.ToList();
GenerateXml(url、asList);
}
及
protectedvoid GenerateXml(字符串url,列表项)//GenerateXml
{
XNSSITEMAP=”http://www.sitemaps.org/schemas/sitemap/0.9";
XnsImage=”http://www.google.com/schemas/sitemap-image/1.1";
var sitemap=新XDocument(新XDeclaration(“1.0”、“UTF-8”和“));
var urlSet=new-XElement(nsSitemap+“urlSet”,
新XAttribute(“xmlns”,nsSitemap),
新的XAttribute(XNamespace.Xmlns+“image”,nsImage),
新XElement(nsSitemap+“url”,
新XElement(nsSitemap+“loc”,url),
从listitems中的urlNode
选择新像素(nsImage+“图像”,
新XElement(nsImage+“loc”,urlNode)
)));
sitemap.Add(urlSet);
Save(System.Web.HttpContext.Current.Server.MapPath(“/Static/sitemaps/sitemap image.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>
<image:image>
<image:loc>http://example.com/photo.jpg</image:loc>
</image:image>
</url>
<url>
<loc>http://example.com/new.html</loc>
<image:image>
<image:loc>http://example.com/newimage.jpg</image:loc>
</image:image>
<image:image>
<image:loc>http://example.com/newphoto.jpg</image:loc>
</image:image>
</url>
</urlset>
http://example.com/sample.html
http://example.com/image.jpg
http://example.com/photo.jpg
http://example.com/new.html
http://example.com/newimage.jpg
http://example.com/newphoto.jpg
但这里我得到了一个url标签。如何做到这一点?有什么建议吗?听起来这实际上只是想在调用
GenerateXml
之前获取所有URL(从所有源文档中),并记住每个URL的来源。这很简单:
var sources = new List<string>
{
"http://example.com/sample.html",
"http://example.com/new.html"
};
var imagesBySource = sources
.ToDictionary(source => source,
source => new HtmlWeb().Load(url)
.DocumentNode.Descendants("img")
.Select(e => e.GetAttributeValue("src", null))
.Where(s => !String.IsNullOrEmpty(s))
.ToList());
GenerateXml(imagesBySource);
请注意,这并不能保证源的顺序得到保留。如果需要,您可能应该创建一个具有
Url
和Images
属性的类,并将这些属性的列表传递给GenerateXml
。您在第一段代码的循环中调用GenerateXml
,因此在最后一次迭代中只使用XML。我怀疑你不想…@Jon Skeet:是的,我尝试过使用dictionary,并在Generate xml outside循环中传递了dictionary值..但我不知道如何获取格式linq@JonSkeet:Dictionary myDict=新字典();添加(url,asList);GenerateXml(myDict);source=>new HtmlWeb().Load(url)抛出以下错误无法将lambda表达式转换为类型“System.Collections.Generic.IEqualityComparer”,因为它不是委托类型,所以我使用了foreach,并且foreach(listvalue中的字符串url){var document=new HtmlWeb().Load(url)工作正常;var url=document.DocumentNode.substands(“img”)。选择(e=>e.GetAttributeValue(“src”,null))。其中(s=>!String.IsNullOrEmpty(s)).ToList();myDict.Add(url,url);}@bala3569:应该可以-应该使用ToDictionary
的第二个参数作为元素选择器。。。将有一个look@bala3569:它为我编译,并进行了一些快速更改,以使用XDocument
而不是HtmlWeb
(我以前从未见过)。
var sources = new List<string>
{
"http://example.com/sample.html",
"http://example.com/new.html"
};
var imagesBySource = sources
.ToDictionary(source => source,
source => new HtmlWeb().Load(url)
.DocumentNode.Descendants("img")
.Select(e => e.GetAttributeValue("src", null))
.Where(s => !String.IsNullOrEmpty(s))
.ToList());
GenerateXml(imagesBySource);
protected void GenerateXml(Dictionary<string, List<string>> imagesByUrl)
{
XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace nsImage = "http://www.google.com/schemas/sitemap-image/1.1";
var sitemap = new XDocument(new XDeclaration("1.0", "UTF-8", ""));
var urlSet = new XElement(nsSitemap + "urlset",
new XAttribute("xmlns", nsSitemap),
new XAttribute(XNamespace.Xmlns + "image", nsImage),
imagesByUrl.Select(entry =>
new XElement(nsSitemap + "url",
new XElement(nsSitemap + "loc", entry.Key),
from urlNode in entry.Value
select new XElement(nsImage + "image",
new XElement(nsImage + "loc", urlNode)
)
)
);
sitemap.Add(urlSet);
var path = HttpContext.Current.Server.MapPath("/Static/sitemaps/Sitemap-image.xml");
sitemap.Save(path);
}