2012-05-11 6 views
0

J'ai du code en utilisant LINQ to XML. Je souhaite écrire ce XElement à chaque fois dans un nouveau fichier XML, de sorte que tout le XML qu'il contient soit stocké dans un fichier XML dans un emplacement spécifié.écriture de plusieurs nœuds xml dans un seul fichier xml

XElement xml = new XElement("contacts", 
        new XElement("contact", 
         new XAttribute("contactId", "2"), 
         new XElement("firstName", "Barry"), 
         new XElement("lastName", "Gottshall") 
        ), 
        new XElement("contact", 
         new XAttribute("contactId", "3"), 
         new XElement("firstName", "Armando"), 
         new XElement("lastName", "Valdes") 
        ) 
       ); 


Console.WriteLine(xml); 

I WSH pour ajouter la variable xml et ajouter plus de noeuds dans, j'ai essayé dans le code suivant, mais n'a pas pu le faire, je veux écrire al xml je reçois dans EveryComment Liste

XElement xml; 
        foreach (string Dcomment in EveryComment) 
        { 
         string commentDetail = "http://www.edmunds.com" + Dcomment; 
         WebClient wc1 = new WebClient(); 
         try { 
          string comm = wc1.DownloadString(commentDetail); 
          string[] time_sep = { @"<time itemprop=""dtreviewed"" datetime=", "</time></div>" }; 
          string[] car_split = {@"<span itemprop=""itemreviewed"">",@"</span><br/>  <div class=""header-5"">Review</div>"}; 
          string[] name_comment_split = {@"<div class=""crr_by"">By <strong itemprop=""reviewer"">","</strong> on <time itemprop=",@"<div class=""header-5"">Review</div>  <span itemprop=""description"">",@"<div class=""header-5"">Favorite Features</div>" }; 
          string[] get_name_comment = comm.Replace("\n", "").Replace("\t", "").Split(name_comment_split, StringSplitOptions.None); 
          string[] get_car_name = comm.Replace("\n","").Replace("\t","").Split(car_split, StringSplitOptions.None); 
          //CAR NAME AT 1 INDEX 
          string perfor,comfi,fuel,fun,interi,exteri,made,reliable; 
          string[] get_time = comm.Split(time_sep, StringSplitOptions.None);//TIME AT 1TH INDEX 
          comm = comm.Replace(@"""", "").Replace("\n"," ").Replace("\t"," ").Replace(" ","").Replace("\r",""); 
          //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
          //doc.LoadHtml(comm); 
          //var links = doc.DocumentNode.Name="span".Select(n => n.["href"].Value).ToArray(); 
          string[] comm_split = { "Performance<hr/><spanclass=ratingtitle=", "Comfort<hr/><spanclass=ratingtitle=", "FuelEconomy<hr/><spanclass=ratingtitle=", "Fun-to-Drive<hr/><spanclass=ratingtitle=", "InteriorDesign<hr/><spanclass=ratingtitle=", "ExteriorDesign<hr/><spanclass=ratingtitle=", "BuildQuality<hr/><spanclass=ratingtitle=", "Reliability</div><spanclass=rating-bigtitle=","<divclass=header-5>FavoriteFeatures</div>", "<divclass=header-5>SuggestedImprovements</div>" }; 
          string[] features = comm.Split(comm_split, StringSplitOptions.None); 
          if (features[1].ElementAt(1) == '.') 
          { 

           perfor = features[1].Substring(0, 3); 
          } 
          else 
           perfor = features[1].Substring(0, 1); 
          if (features[2].ElementAt(1) == '.') 
          { 

           comfi = features[2].Substring(0, 3); 
          } 
          else 
           comfi = features[2].Substring(0, 1); 
          if (features[3].ElementAt(1) == '.') 
          { 

           fuel = features[3].Substring(0, 3); 
          } 
          else 
           fuel = features[3].Substring(0, 1); 
          if (features[4].ElementAt(1) == '.') 
          { 

           fun = features[5].Substring(0, 3); 
          } 
          else 
           fun = features[5].Substring(0, 1); 
          if (features[6].ElementAt(1) == '.') 
          { 

           interi = features[6].Substring(0, 3); 
          } 
          else 
           interi = features[6].Substring(0, 1); 

          if (features[7].ElementAt(1) == '.') 
          { 

           exteri = features[7].Substring(0, 3); 
          } 
          else 
           exteri = features[7].Substring(0, 1); 

          if (features[8].ElementAt(1) == '.') 
          { 

           reliable = features[8].Substring(0, 3); 
          } 
          else 
           reliable = features[8].Substring(0, 1); 


       xml = new XElement("DOC", 
        new XAttribute("Relevance", c1.relevance), 
        new XElement("Date", get_time[1]), 
        new XElement("Author", get_name_comment[1]), 
        new XElement("Text", get_name_comment[3]), 
        new XElement("Favourite",features[9]), 
        new XElement("Peformance",perfor), 
        new XElement("Fuel",fuel), 
        new XElement("Fun",fun), 
        new XElement("Interior",interi), 
        new XElement("Exterior",exteri), 
        new XElement("Reliability",reliable) 
       ); 
         // xml = new XElement(get_car_name 

         } 
         catch (Exception ex) 
         { 
         } 
        } 
+0

Vous voulez conserver ce xml dans un fichier? – gideon

+0

Quelle est votre question? Qu'est-ce qui ne fonctionne pas? –

+0

J'ai ajouté plus de code que je souhaite concaténer le xml dans chaque itération et après la boucle je l'enregistrerai dans le fichier –

Répondre

0

Je l'ai fait de cette façon chaque xelemnt je me suis converti en chaîne à la fin de la boucle et continuer à concating cette chaîne, après boucle je l'ai écrit dans dans un fichier .xml par chaîne écrivain

2
xml.Save(path); 

ou y avait-il quelque chose de plus subtil impliqué, ce n'est pas dans la question?

+0

J'ai ajouté plus de code que je souhaite concaténer le xml chaque itération et après la boucle je vais l'enregistrer dans le fichier –

+0

@RobustRob vous ne pouvez pas vraiment concaténer des documents XML, en raison du besoin d'un seul élément racine externe. Cela laisse 2 options: réécrire * tout * chaque sauvegarde, ou: utiliser quelque chose comme 'XmlWriter' pour écrire dans un flux avant seulement –

+0

tout exemple de bonjour sera très utile particulier à mon cas –

Questions connexes