2015-03-07 3 views
0

Im пытается прочитать некоторые атрибуты из следующих XML-файл (как консольная программа)C# Чтение определенного элемента/атрибута из XML-файла

http://api.openweathermap.org/data/2.5/forecast?q=lahti,fin&mode=xml

Как вы видите, в «прогноз» элемента Есть несколько 'время' элементов. Я хочу выбрать определенный элемент 'time', а затем выбрать нужную вещь внутри (скажем 'symbol') и распечатать все/любые атрибуты, которые он имеет.

Я хочу иметь возможность контролировать, какой «время» Элемент, который я выбираю, и какие атрибуты я хочу распечатать.

Это далеко все, что мне удалось сделать, это напечатать все «время» элемента и их атрибуты, а также мне удалось напечатать каждый атрибут внутри данного «времени» элемента. Но я просто не могу понять, как его контролировать.

Со следующим кодом я могу распечатать все, что находится внутри первого 'время' элемент. Элемент (0) - это индекс элемента, а цикл for гарантирует, что я не получу пустые строки. Как вы можете видеть из xml-файла, некоторые элементы 'time' имеют различное количество атрибутов внутри них, поэтому, я думаю, мне нужно их назвать По имени insted of index.

static void xmlReader() 
    { 
     int i; 

     XmlDocument xmlDoc = new XmlDocument(); 
     xmlDoc.Load(parseLink()); 

     foreach (XmlNode xmlNode in xmlDoc.DocumentElement.GetElementsByTagName("time").Item(0)) 
      for (i = 0; i < xmlNode.Attributes.Count; i++) 
      { 
       Console.WriteLine(xmlNode.Attributes[i].Value); 
      } 
     Console.ReadKey(); 

    } 

ответ

1

Используйте Linq2Xml, это намного проще и удобнее.

public static void Main() 
{ 
    var forecast = XDocument.Load(@"http://api.openweathermap.org/data/2.5/forecast?q=lahti,fin&mode=xml") 
          .Root 
          .Element("forecast"); 

    foreach (var time in forecast.Elements("time") 
           .Where(e => e.Element("clouds") 
               .Attribute("value") 
               .Value == "overcast clouds")) 
    { 
     Console.WriteLine(time.Element("symbol").Attribute("name").Value); 
    } 
} 
+0

https://dotnetfiddle.net/Lbgka3 – aush

0

Вы можете использовать XmlReader (tutorial), как течет,

Здесь я получаю from атрибут из time элемента и name атрибута sybol элемента .. И они за это же время элемент. Также добавлена ​​извлекающий пример значения облака

using (XmlReader reader = XmlReader.Create(@"http://api.openweathermap.org/data/2.5/forecast?q=lahti,fin&mode=xml")) 
{ 
     // Walk through all Elements 
     while (reader.Read()) 
     { 
     // If we meet time element ; go inside and walk 
     if (reader.Name == "time") 
     { 
       Console.WriteLine("A new TIME "); 

       // Extract from attribute 
       String from = reader["from"]; 
       if (from != null) 
       { 
        Console.WriteLine("\t From : " + from); 
       } 

       // Now walk through all elements inside same Time element 
       // Here I use do-while ; what we check is End element of time : </time> .. when we walk till we meet </time> we are inside children of same Time 
       // That mean we start from <time> and walk till we meet </time> 
       do 
       { 
        reader.Read(); 

        if (reader.Name == "symbol") 
        { 
         // You can use this approach for any Attribute in symbol Element 
         String name = reader["name"]; 
         if (name != null) 
         { 
           Console.WriteLine("\t Symbol name :" + name); 
         } 
        } 

        if (reader.Name == "clouds") 
        { 
         String clouds = reader["value"]; 
         if (clouds != null) 
         { 
          Console.WriteLine("\t\t Clouds value : " + clouds); 
         } 
        } 

       } while (reader.NodeType != XmlNodeType.EndElement && reader.Name != "time"); 

      } 
     } 
} 

Просто попробовать это на вашей программе консоли ..

0

ответ Подобное @ aush, но с некоторым форматированием

 var doc = XDocument.Load(@"http://api.openweathermap.org/data/2.5/forecast?q=lahti,fin&mode=xml"); 
     var forecastEl = doc.Root.Element(XName.Get("forecast")); 

     var timeNodes = from e in forecastEl.Elements("time") 
         where e.Element("symbol") 
          .Attribute(XName.Get("name")) 
          .Value == "light snow" 
         select e; 

     var colFormat = "{0,-20} {1,-20} {2,-30}"; 
     Console.WriteLine(colFormat, "TimeFrom", "TimeTo", "SymbolName"); 
     foreach(var time in timeNodes) 
      Console.WriteLine(colFormat 
       , time.Attribute("from").Value 
       , time.Attribute("to").Value 
       , time.Element("symbol").Attribute("name").Value); 

Результаты:

TimeFrom    TimeTo    SymbolName 
2015-03-07T12:00:00 2015-03-07T15:00:00 light snow 
2015-03-07T15:00:00 2015-03-07T18:00:00 light snow 
0

Использование XDocument здесь немного проще ...

private static void XmlOutput() 
    { 
     var filePathAndName = @"http://api.openweathermap.org/data/2.5/forecast?q=lahti,fin&mode=xml"; 
     var xmlDoc = XDocument.Load(filePathAndName); 

     // Get list of Nodes matching the "time" name or any other filters you wish. 
     var nodes = xmlDoc.Descendants().Where(nd => nd.Name.LocalName == "time"); 

     // Filter the node list to only those where the date is as specified (and any other filters you wish). 
     // This could be done in the initial linq query - just making it clearer here. 
     nodes = nodes.Where(nd => nd.Attributes().Any(cnd => cnd.Name.LocalName == "from" && cnd.Value.Contains("2015-03-07"))); 

     foreach (XElement element in nodes) 
     { 
      // For each descendant node where named "symbol"... 
      foreach(var node in element.Descendants().Where(nd => nd.Name.LocalName == "symbol")) 
      { 
       // Write out these particular attributes ("number" and "name") 
       string output = ""; 
       output += node.Attributes().FirstOrDefault(nd => nd.Name.LocalName == "number").Value; 
       output += ", " + node.Attributes().FirstOrDefault(nd => nd.Name.LocalName == "name").Value; 
       Console.WriteLine(output); 
      } 
     } 

     Console.ReadKey(); 
    } 
0

Используя мой XML library here, вы можете искать фактического DateTime значения, как это:

XElement root = XElement.Load(@"http://api.openweathermap.org/data/2.5/forecast?q=lahti,fin&mode=xml"); 

var search = DateTime.Parse("2015-03-07T21:00:00"); 

XElement time = root.XPathElement("//time[@from >= {0} and @to > {1}]", search, search); 

var value = time.ToString(); 

enter image description here

Смежные вопросы