C# 使用.degents(值)解析XML文档

C# 使用.degents(值)解析XML文档,c#,.net,xml,xml-parsing,C#,.net,Xml,Xml Parsing,我正在尝试解析我创建的xml文档。但是,如果值包含某些字符(包括空格),则xml.degenantsvalue不起作用,这是我的问题 我的xml的结构如下: <stockists> <stockistCountry country="Great Britain"> <stockist> <name></name> <address></address> </sto

我正在尝试解析我创建的xml文档。但是,如果值包含某些字符(包括空格),则xml.degenantsvalue不起作用,这是我的问题

我的xml的结构如下:

<stockists>
  <stockistCountry country="Great Britain">
    <stockist>
      <name></name>
      <address></address>
    </stockist>
  </stockistCountry>
  <stockistCountry country="Germany">
    <stockist>
      <name></name>
      <address></address>
    </stockist>
  </stockistCountry>
  ...
</stockists>
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);
用于解析的C代码如下所示:

<stockists>
  <stockistCountry country="Great Britain">
    <stockist>
      <name></name>
      <address></address>
    </stockist>
  </stockistCountry>
  <stockistCountry country="Germany">
    <stockist>
      <name></name>
      <address></address>
    </stockist>
  </stockistCountry>
  ...
</stockists>
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解析,属性中是否应该有空格等等?如何修复它,以便英国能够

但是,如果值具有某些字符,则xml.degenantsvalue不起作用

后代希望标记使用XName,而不是值

XML标记确实不允许包含空格

但是,示例XML只包含一个属性的值,并且空间很好

更新:

我想你需要

//var stockist = from s in xml.Descendants(stockistCountry.FirstAttribute.Value) 
//              select s;

var stockists = stockistCountry.Descendants("stockist");
但是,如果值具有某些字符,则xml.degenantsvalue不起作用

后代希望标记使用XName,而不是值

XML标记确实不允许包含空格

但是,示例XML只包含一个属性的值,并且空间很好

更新:

我想你需要

//var stockist = from s in xml.Descendants(stockistCountry.FirstAttribute.Value) 
//              select s;

var stockists = stockistCountry.Descendants("stockist");

请描述一下它不起作用。这看起来基本上没问题。@Henkholtman在网上评论说,我现在看到的空间是不允许的,但那该怎么办?它正在寻找一个元素。@HenkHolterman,但它失败了,因为它有一个空格inDescribe,请不要用。这看起来基本上没问题。@Henkholtman在网上评论说,我现在看到的空间是不允许的,但那该怎么办?它正在寻找一个元素。@HenkHolterman,但它失败了,因为它在中有一个空格