C# 查询XML文件
我应该在我的C应用程序中解析XML文件 这是我的XML文件的一个示例C# 查询XML文件,c#,.net,xml,C#,.net,Xml,我应该在我的C应用程序中解析XML文件 这是我的XML文件的一个示例 <?xml version="1.0" encoding="utf-8" ?> <tokens> <token id="1" > <id>1</id> <nome>"Pippo"</nome> <numeroSillabe>"2"</numeroSillabe>
<?xml version="1.0" encoding="utf-8" ?>
<tokens>
<token id="1" >
<id>1</id>
<nome>"Pippo"</nome>
<numeroSillabe>"2"</numeroSillabe>
<sillabaIniziale>"Pip"</sillabaIniziale>
</token>
<token id="2">
<id>2</id>
<nome>Pluto</nome>
<numeroSillabe>2</numeroSillabe>
<sillabaIniziale>Plu</sillabaIniziale>
</token>
<token id="3">
<id>3</id>
<nome>Palla</nome>
<numeroSillabe>2</numeroSillabe>
<sillabaIniziale>Pa</sillabaIniziale>
</token>
</tokens>
为了读取此文件,我使用以下代码
XmlDocument document = new XMLDocument()
document.Load("Content\\Token.xml");
// arrivo al nodo
//XmlNode node = document.DocumentElement.SelectSingleNode("/tokens/token");
//dichiaro la mappa per i token
Dictionary<string, Token> mappa = new Dictionary<string, Token>();
foreach (XmlNode node in document.DocumentElement.ChildNodes)
{
//creo l'oggetto Token e lo popolo con i risultati del parse
Token token = new Token();
//compilo il campo ID
token.setId((node["id"].InnerText).Replace("\"", ""));
//compilo il nome
token.setNome((node["nome"].InnerText).Replace("\"", ""));
//compilo l numero sillabe
token.setNumeroSillabe((node["numeroSillabe"].InnerText).Replace("\"", ""));
//compilo la sillaba iniziale
token.setSillabaIniziale(node["sillabaIniziale"].InnerText);
//aggiungo all array il token
mappa.Add(token.getId(), token);
}
它起作用了
现在我想从XML文档中搜索元素,例如,从文档中选择*fromDocument\uXML,其中numeroSilabe=2
如何执行此任务
List<Token> tokens = mappa.Select(x=>x.Values.Where(y=>y.getNumeroSillabe()==2)).ToList();
进行此更改后,上述linq查询的代码更改为:
List<Token> tokens = mappa.Select(x=>x.Values.Where(y=>y.NumeroSillabe==2)).ToList();
其他三项作业也是如此
有关重新分级自动实现的属性的进一步阅读,请查看以下链接:
进行此更改后,上述linq查询的代码更改为:
List<Token> tokens = mappa.Select(x=>x.Values.Where(y=>y.NumeroSillabe==2)).ToList();
其他三项作业也是如此
有关重新分级自动实现的属性的进一步阅读,请查看以下链接:
如果您需要在XML文件中导航,如代码示例中所示,可以使用以下方法:
var mappa = document.SelectNodes("/tokens/token[numeroSillabe=2]") // Filter
.OfType<XmlElement>()
.ToDictionary(
t => t.GetAttribute("id"),
t => new
{
Id = t.GetAttribute("id"),
Nome = t.SelectSingleNode("nome" ).InnerText,
NumeroSillabe = t.SelectSingleNode("numeroSillabe" ).InnerText,
SillabaIniziale = t.SelectSingleNode("sillabaIniziale").InnerText
});
如果您需要在XML文件中导航,如代码示例中所示,可以使用以下方法:
var mappa = document.SelectNodes("/tokens/token[numeroSillabe=2]") // Filter
.OfType<XmlElement>()
.ToDictionary(
t => t.GetAttribute("id"),
t => new
{
Id = t.GetAttribute("id"),
Nome = t.SelectSingleNode("nome" ).InnerText,
NumeroSillabe = t.SelectSingleNode("numeroSillabe" ).InnerText,
SillabaIniziale = t.SelectSingleNode("sillabaIniziale").InnerText
});
使用以下命令:
var mappa = document.SelectNodes("//token[numeroSillabe=2]");
使用以下命令:
var mappa = document.SelectNodes("//token[numeroSillabe=2]");
请尝试以下代码,以获取不带任何循环的筛选节点:
var document = new XmlDocument();
document.Load("Content\\Token.xml");
var filteredNodes = document.SelectNodes("tokens/token/numeroSillabe=2");
请尝试以下代码,以获取不带任何循环的筛选节点:
var document = new XmlDocument();
document.Load("Content\\Token.xml");
var filteredNodes = document.SelectNodes("tokens/token/numeroSillabe=2");
搜索使用LINQ到XML的示例搜索使用LINQ到XML的示例谢谢您的回复。我已经把这个代码列表tokens=mappa.Selectx=>x.Values.Wherey=>y.getNumeroSillabe==2.ToList;但我有一个来自VisualStudio的错误。Errore 50'System.Collections.Generic.KeyValuePair'non contiene una definizione di'Values'e nonèstato trovato alcun metodo di estension'Values'che accetta un primo argometo di tipo'System.Collections.Generic.KeyValuePair'。在联合国大会上使用武器的试验。谢谢你的回复。我已经把这个代码列表tokens=mappa.Selectx=>x.Values.Wherey=>y.getNumeroSillabe==2.ToList;但我有一个来自VisualStudio的错误。Errore 50'System.Collections.Generic.KeyValuePair'non contiene una definizione di'Values'e nonèstato trovato alcun metodo di estension'Values'che accetta un primo argometo di tipo'System.Collections.Generic.KeyValuePair'。使用联合国武器和联合国大会的证据。