Reputation: 178
I am trying to show by console the result of a search with LINQ, when debugging I find that the query works, but I do not know how to show the result
I have class XMLReader where I load the xml file, I read it and I do the query
class XMLReader
{
public List<airlines> leerXML()
{
Console.WriteLine("Enter the airline you wish to search: ");
string name;
name= Console.ReadLine().ToUpper();
if (nombre == "V"){
XElement info = XElement.Load(@"C:\Users\thoma\Documents\Visual Studio 2019\Backup Files\data.xml");
IEnumerable<XElement> airlines =
from el in info.Elements("airline")
where (string)el.Element("name") == "HK"
select el;
foreach (XElement el in airlines)
Console.WriteLine((string)el.Attribute("origin"));
}
return null;
}
}
then I have the Program class where I will show the information with a Console.WriteLine ();
class Program
{
static void Main(string[] args)
{
Console.WriteLine("show information: " ??????);
Console.ReadLine();
}
}
here is the xml
<airlines>
<airline id="01">
<name>Viva Colombia</name>
<origin>BOG</origin>
<destination>MDE</destination>
<date>01/03/2019</date>
</airline>
<airline id="02">
<name>HK Express</name>
<origin>BOG</origin>
<destination>CTG</destination>
<date>01/03/2019</date>
</airline>
<airline id="03">
<name>Volotea</name>
<origin>PEI</origin>
<destination>BOG</destination>
<date>01/03/2019</date>
</airline>
<airline id="04">
<name>Vueling</name>
<origin>MDE</origin>
<destination>BOG</destination>
<date>01/03/2019</date>
</airline>
</airlines>
Upvotes: 2
Views: 364
Reputation: 821
If i understood you correctly I guess you need to change your leerXML() method. It will print origin where name is like you entered. Please add null reference check where it is needed.
public void leerXML()
{
Console.WriteLine("Enter the airline you wish to search: ");
string name;
name = Console.ReadLine().ToUpper();
if (!String.IsNullOrEmpty(name))
{
XElement info = XElement.Load(@"C:\Users\thoma\Documents\Visual Studio 2019\Backup Files\data.xml");
var airlines = info.XPathSelectElements("airline");
foreach (XElement el in airlines)
{
if (!String.IsNullOrEmpty(el.Element("name").Value) && ((string)el.Element("name").Value).IndexOf(name) >= 0)
{
Console.WriteLine((string) el.Element("origin").Value);
}
}
}
}
static void Main(string[] args)
{
XMLReader xmlReader = new XMLReader()
xmlReader.leerXML();
Console.ReadLine();
}
Upvotes: 2
Reputation: 367
public static void ReadXmlFile()
{
XDocument doc = XDocument.Load(@"xmlfiledados.xml");
XElement element = doc.Element("airlines").Descendants("airline").Where(a => a.Element("name").Value.Equals("HK Express")).First();
Console.WriteLine(element.Element("name").Value);
Console.WriteLine(element.Element("origin").Value);
Console.WriteLine(element.Element("destination").Value);
Console.WriteLine(element.Element("date").Value);
}
Upvotes: 0
Reputation: 621
One way of doing it:
First of all add the construcror to class XMLReader, so it looks like this:
public class XMLReader
{
public XMLReader()
{
}
public List<airlines> leerXML()
{
Console.WriteLine("Enter the airline you wish to search: ");
string name;
name= Console.ReadLine().ToUpper();
if (nombre == "V"){
XElement info = XElement.Load(@"C:\Users\thoma\Documents\Visual Studio 2019\Backup Files\data.xml");
IEnumerable<XElement> airlines =
from el in info.Elements("airline")
where (string)el.Element("name") == "HK"
select el;
foreach (XElement el in airlines)
Console.WriteLine((string)el.Attribute("origin"));
}
return null;
}
}
You already printing the xml in leerXML() Just call it in your Main(string[] args)
static void Main(string[] args)
{
XMLReader xmlReader = new XMLReader()
xmlReader.leerXML();
Console.ReadLine();
}
That's it.
Upvotes: 0