2009-06-02 3 views
0

Je suis à la recherche d'un bon moyen de sauvegarder/charger ce qui suit. Je veux enregistrer en XML et idéalement en cherchant à utiliser LiNQ (à savoir pour m'aider à apprendre LINQ)LiNQ imbriqué en XML

Je ne sais pas comment faire linq écrit bien. Quelqu'un peut-il aider? Essentiellement, ErrorList contient une liste d'erreurs de niveau supérieur, chaque erreur peut avoir des enfants. La sortie XML devrait ressembler à:

<ErrorList> 
<ErrorName1 Ignore="false"> 
<ChildErrorName1 Ignore="true"> 
<ChildErrorName2 Ignore="false" /> 
</ChildErrorName1> 
</ErrorName1> 
<ErrorList> 

Si quelqu'un pouvait aider ce serait génial. Merci

Répondre

1

D'accord, je pense que je vois ce que vous cherchez maintenant. Essayez ceci:

// Need to declare in advance to call within the lambda. 
Func<ErrorType, XElement> recursiveGenerator = null; 
recursiveGenerator = error => new XElement 
    (error.Name, 
    new XAttribute("Ignore", error.Ignore), 
    error.ChildErrors.Select(recursiveGenerator)); 

var errorList = new XElement 
    ("ErrorList", errors.ChildErrors.Select(recursiveGenerator)); 

Voici un exemple complet:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Xml.Linq; 

public class ErrorType 
{ 
    public String Name { get; set; } 
    public bool Ignore { get; set; } 

    public List<ErrorType> ChildErrors { get; protected set; } 
    public ErrorType() 
    { 
     ChildErrors = new List<ErrorType>(); 
    } 
} 

public class ErrorList 
{ 
    public List<ErrorType> ChildErrors { get; protected set; } 
    public ErrorList() 
    { 
     ChildErrors = new List<ErrorType>(); 
    } 
} 

public class Test 
{ 
    public static void Main() 
    { 
     var childError2 = new ErrorType { 
      Name = "ChildErrorName2", Ignore=false }; 
     var childError1 = new ErrorType { 
      Name = "ChildErrorName1", Ignore=true, 
      ChildErrors = { childError2 } 
     }; 
     var mainError = new ErrorType { 
      Name = "ErrorName1", Ignore=true, 
      ChildErrors = { childError1 } 
     }; 
     var errorList = new ErrorList { ChildErrors = { mainError } }; 

     // Need to declare in advance to call within the lambda. 
     Func<ErrorType, XElement> recursiveGenerator = null; 
     recursiveGenerator = error => new XElement 
      (error.Name, 
      new XAttribute("Ignore", error.Ignore), 
      error.ChildErrors.Select(recursiveGenerator)); 

     var element = new XElement 
      ("ErrorList", 
       errorList.ChildErrors.Select(recursiveGenerator); 

     Console.WriteLine(element); 
    }   
} 
+0

Vive Jon qui semble à peu près juste. Je l'ai testé cependant et il a une erreur de compilation sur la ligne: errorList.ChildErrors.Select (recursiveGenerator (x)); J'ai remplacé X par errorList.ChildErrors [0] pour correspondre au type correct mais pas de joie. Mes compétences lambda sont faibles, donc je ne pouvais pas comprendre ce qui n'allait pas. – Chris

+0

Oups, fixé cela - il devrait être errorList.ChildErrors.Select (recursiveGenerator); –

+0

(J'avais l'habitude d'avoir errorList.ChildErrors.Select (x => recursiveGenerator (x)) qui est un peu redondant.) –

0

Regardez this. Ce devrait être ce que vous cherchez. Pas de pensée LINQ mais plutôt une manière simple et agréable de sortir du XML.

La ligne du bas est cependant d'utiliser .