2011-08-28 17 views
8

Ho il seguente file xmlleggere il file XML utilizzando LINQ

<?xml version="1.0" encoding="utf-8"?> 
<Users> 
    <User> 
     <Name>John Smith</Name> 
     <test> 
      <Date>23.05.2011</Date> 
      <points>33</points> 
     </test> 
     <test> 
      <Date>22.06.2011</Date> 
      <points>29</points> 
     </test> 
    </User> 
</Users> 

e vorrei utilizzare LINQ per estrarre le date e le punte delle prove in cui nome utente è "John Smith" ..

come potrei costruire il mio linq?

ho fatto quanto segue, ma non funziona come vorrei:

XElement main = XElement.Load(@"users.xml"); 

string t = "John Smith"; 
var v = from user in main.Elements("User") 
     where t == users.Element("Name").Value 
     select users; 

MessageBox.Show(v.First().Element("Date").Value.ToString()); 
+1

Il tuo codice non dovrebbe essere compilato. Stai usando 'utenti' dove dovresti usare' utente'. E hai errori di battitura nella tua inizializzazione 't'. – svick

risposta

7

Non sono sicuro di quale formato si desidera che l'uscita sia, ma questo codice campioni devono ottenere la data e punti. Questo proietta i risultati in un tipo anonimo:

class Program 
{ 
    static void Main(string[] args) 
    { 
     XElement main = XElement.Load(@"users.xml"); 

     var results = main.Descendants("User") 
      .Descendants("Name") 
      .Where(e => e.Value == "John Smith") 
      .Select(e => e.Parent) 
      .Descendants("test") 
      .Select(e => new { date = e.Descendants("Date").FirstOrDefault().Value, points = e.Descendants("points").FirstOrDefault().Value }); 

     foreach (var result in results) 
      Console.WriteLine("{0}, {1}", result.date, result.points); 
     Console.ReadLine(); 
    } 
} 

e l'uscita è:

23.05.2011, 33 
22.06.2011, 29 
+0

GREAT .. grazie come inserirò un nuovo nodo "in questo esempio un terzo" con data e punti per quell'utente (John Smith) nel file xml? – brandon

+0

@JohnD Questo è stato super utile. Grazie. –

1
XDocument main = XDocument.Load(@"users.xml"); 

string t = "John Smith"; 
var v = from user in main.Descendants("User") 
    where t == user.Element("Name").Value 
    select user; 

MessageBox.Show(v.First().Element("Date").Value.ToString()); 

dovrebbe fare il trucco.

1

Prova questo fuori

class Program 
{ 
    static void Main(string[] args) 
    { 
     XElement main = XElement.Parse(
@"<Users> 
    <User> 
     <Name>John Smith</Name> 
     <test> 
      <Date>23.05.2011</Date> 
      <points>33</points> 
     </test> 
     <test> 
      <Date>22.06.2011</Date> 
      <points>29</points> 
     </test> 
    </User> 
</Users>"); 

     var users = 
      from m in main.Elements("User") 
      where (string)m.Element("Name") == "John Smith" 
      select (m.Descendants("test").Descendants("Date").FirstOrDefault().Value); 
     foreach (var user in users) 
      Console.WriteLine(user); 
     Console.ReadLine(); 
    } 
} 

saluti

0

E sulla tua altra domanda per aggiungere un altro nodo a John Smith, questa sarebbe la soluzione:

class Program 
{ 
    static void Main(string[] args) 
    { 
     XElement main = XElement.Parse(
    @"<Users> 
     <User> 
      <Name>Alex</Name> 
      <test> 
       <Date>08.05.2011</Date> 
       <points>4</points> 
      </test> 
     </User> 
     <User> 
      <Name>John Smith</Name> 
      <test> 
       <Date>23.05.2011</Date> 
       <points>33</points> 
      </test> 
      <test> 
       <Date>22.06.2011</Date> 
       <points>29</points> 
      </test> 
     </User> 
    </Users>"); 


    var users = 
     from m in main.Elements("User") 
     where (string)m.Element("Name") == "John Smith" 
     select (m.Descendants("test").Descendants("Date").FirstOrDefault().Value); 

    XElement Mercury = main.Elements("User").Where(p => (String)p.Element("Name") == "John Smith").FirstOrDefault(); 
    Mercury.Add(new XElement("test", new XElement("Date", "06.06.2011"), new XElement("points", "01"))); 

    foreach (var user in main.Elements()) 
     Console.WriteLine(user); 

    Console.ReadLine(); 
} 

}

Dando il prossimo risultato previsto:

<User> 
    <Name>Alex</Name> 
    <test> 
    <Date>08.05.2011</Date> 
    <points>4</points> 
    </test> 
</User> 
<User> 
    <Name>John Smith</Name> 
    <test> 
    <Date>23.05.2011</Date> 
    <points>33</points> 
    </test> 
    <test> 
    <Date>22.06.2011</Date> 
    <points>29</points> 
    </test> 
    <test> 
    <Date>06.06.2011</Date> 
    <points>01</points> 
    </test> 
</User> 
Problemi correlati