2014-11-28 4 views
1

У меня есть этот код, чтобы забрать еду и foodplaces из XML Att первым я сохранил имя каждого restuarnt в списке строк и я хочу, чтобы выбрать пищу, это рестораны в списках reservsВыберите элемент из XML

foreach(var restaurant in restaurants) 
     { 
      List<foodplace> foodPlaces = (from _foodplaces in xmlDocument.Element("foodplaces").Elements("foodplace") 
              where _foodplaces.Value == restaurant 
              select new foodplace 
              { 
               day = (from day in _foodplaces.Elements(thisDay) 
                let list = day.Elements("food").Take(3).ToList() 
                select new DayOfWeek 
                { 
                 food1 = (list.Count > 0 ? list[0].Value : string.Empty), 
                 food2 = (list.Count > 1 ? list[1].Value : string.Empty), 
                 food3 = (list.Count > 2 ? list[2].Value : string.Empty) 
                }).FirstOrDefault() 
              }).ToList(); 

проблема в том, что _foodplaces значение возвращается из xmldoxument, что lookslike это

\n\n\n\t the litle inddian \t\n\n\n 

и значение Restuarant это строка выглядит этот «Лил индийский»

и поэтому оператор LINQ возвращает нуль becuse _foodplace не Restuarant как прийти Arround этой

ответ

0

Чтобы удалить пробелы и, возможно, рассмотреть случай чувствительность:

where String.Equals(_foodplaces.Value.Trim(), restaurant, StringComparison.OrdinalIgnoreCase) 
0
string nameToSearch = "Restaurant12"; 
string xml = File.ReadAllText(<<pathtoxml>>, Encoding.Default); 

XmlDocument doc = new XmlDocument(); 
doc.LoadXml(xml); 
//this depends on your xml structure: i assumed that the attribute of the restaurant is name 
XmlNode node = doc.selectSingleNode("foodplaces/foodplace[@name = '"+nameToSearch +"']"); 
if(node != null){ 
    foreach(XmlNode foodNode in node.selectNodes("food")){ 
     //Do something with the foodnode 
     //e.g. foodNode.SelectsingleNode("Price").InnerText 
    } 
} 
0

Просто установите PreserveWhitespace свойство XMLDocument до false перед загрузкой xml.

XmlDocument doc = new XmlDocument(); 
doc.PreserveWhitespace = false; 
doc.Load("your.xml"); 
Смежные вопросы