我试图通过控制台显示使用LINQ进行搜索的结果,在调试时我发现该查询有效,但是我不知道如何显示结果
我有XMLReader类,可以在其中加载xml文件,读取并执行查询
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;
}
}
然后有了Program类,在其中将使用Console.WriteLine();来显示信息。
class Program
{
static void Main(string[] args)
{
Console.WriteLine("show information: " ??????);
Console.ReadLine();
}
}
这是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>
答案 0 :(得分:2)
如果我对您的理解正确,我想您需要更改leerXML()方法。 它将在您输入姓名的地方打印原点。 请在需要的地方添加空引用检查。
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();
}
答案 1 :(得分:0)
一种实现方式:
首先将construcror添加到XMLReader类中,因此它看起来像这样:
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;
}
}
您已经在leerXML()中打印了xml 只需在您的Main(string [] args)
中调用它即可 static void Main(string[] args)
{
XMLReader xmlReader = new XMLReader()
xmlReader.leerXML();
Console.ReadLine();
}
就是这样。
答案 2 :(得分:0)
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);
}