我想解析我创建的xml文档。但xml.Descendants(value)
不起作用,如果value
有某些字符(包括空间,这是我的问题)。使用.Descendents解析XML文档(值)
我的XML的结构是这样的:
<stockists>
<stockistCountry country="Great Britain">
<stockist>
<name></name>
<address></address>
</stockist>
</stockistCountry>
<stockistCountry country="Germany">
<stockist>
<name></name>
<address></address>
</stockist>
</stockistCountry>
...
</stockists>
而对于分析我的C#代码如下所示:
string path = String.Format("~/Content/{0}/Content/Stockists.xml", Helper.Helper.ResolveBrand());
XElement xml = XElement.Load(Server.MapPath(path));
var stockistCountries = from s in xml.Descendants("stockistCountry")
select s;
StockistCountryListViewModel stockistCountryListViewModel = new StockistCountryListViewModel
{
BrandStockists = new List<StockistListViewModel>()
};
foreach (var stockistCountry in stockistCountries)
{
StockistListViewModel stockistListViewModel = new StockistListViewModel()
{
Country = stockistCountry.FirstAttribute.Value,
Stockists = new List<StockistDetailViewModel>()
};
var stockist = from s in xml.Descendants(stockistCountry.FirstAttribute.Value) // point of failure for 'Great Britain'
select s;
foreach (var stockistDetail in stockist)
{
StockistDetailViewModel stockistDetailViewModel = new StockistDetailViewModel
{
StoreName = stockistDetail.FirstNode.ToString(),
Address = stockistDetail.LastNode.ToString()
};
stockistListViewModel.Stockists.Add(stockistDetailViewModel);
}
stockistCountryListViewModel.BrandStockists.Add(stockistListViewModel);
}
return View(stockistCountryListViewModel);
,我想知道如果我接近的Xml正确地分析是否我不该在我的属性等没有空格?如何解决它,这样英国将解析
说明 “不工作” 请。这看起来基本上可以。 – 2012-04-19 10:11:27
@HenkHolterman在线评论失败,说不允许空格 – ediblecode 2012-04-19 10:17:10
我现在看到了,但是应该怎么做?它正在寻找一个“大不列颠”元素。 – 2012-04-19 10:19:54