locked
Updating XML File - Elements between two start elements RRS feed

  • Question

  • Hey all

     

    I've been messing around with various ways of writing this for the past two hours, however between the visited tags below, things keep screwing up between the visited tags, no matter what I change, heres how each entry should look:

     

     

    - <websites>
     <sitename>Test Site</sitename> 
     <siteuri>http://www.test.com</siteuri> 
     <linksgained>4493</linksgained> 
    - <visited>
     <uri>http://www.google.co.uk</uri> 
     <uri>http://www.yahoo.com</uri> 
     <uri>http://www.google.co.uk</uri> 
     <uri>http://www.yahoo.com</uri> 
     <uri>http://www.google.co.uk</uri> 
     <uri>http://www.yahoo.com</uri> 
     <uri>http://www.google.co.uk</uri> 
     <uri>http://www.yahoo.com</uri> 
     </visited>
     </websites>

     Here's the code I'm using, obviously the for each loop is most of the problem, I'm probably doing something wrong as I'm new to XML

     

     

    private static List<string> _uris = new List<string>();
        private static XmlElement _usedUris;
    
    public static void Main(string[] args)
        {
          _uris.Add("http://www.google.co.uk");
          _uris.Add("http://www.yahoo.com");
          _uris.Add("http://www.google.co.uk");
          _uris.Add("http://www.yahoo.com");
          _uris.Add("http://www.google.co.uk");
          _uris.Add("http://www.yahoo.com");
          _uris.Add("http://www.google.co.uk");
          _uris.Add("http://www.yahoo.com");
    
    
          
       AddToXmlFile();
          
    
    
        }
    
     private static void AddToXmlFile()
        {
          XmlDocument doc = new XmlDocument();  
    
          doc.Load("websitemanager.xml");
    
          XmlElement siteName = doc.CreateElement("sitename");
          siteName.InnerText = "My site";
          doc.DocumentElement.AppendChild(siteName);
    
          XmlElement siteUri = doc.CreateElement("siteuri");
          siteUri.InnerText = "http://www.msn.com";
          doc.DocumentElement.AppendChild(siteUri);
    
          XmlElement links = doc.CreateElement("linksgained");
          links.InnerText = "3333";
          doc.DocumentElement.AppendChild(links);
    
          XmlElement visiteduris = doc.CreateElement("visited");
          doc.DocumentElement.AppendChild(visiteduris);
    
          foreach (string s in _uris)
          {
            _usedUris = doc.CreateElement("uri");
            _usedUris.InnerText = s;
            doc.DocumentElement.AppendChild(_usedUris);
          }
    
          visiteduris.AppendChild(_usedUris);
    
          XmlTextWriter writer = new XmlTextWriter("websitemanager.xml", null);
          writer.Formatting = Formatting.Indented;
          doc.WriteContentTo(writer);
          writer.Flush();
          writer.Close();
          
    
        }
     Apologies for such a trivial question, but I just cant see it.

     

    Sunday, July 4, 2010 8:33 PM

Answers

  • You are appending the _usedUris element outside the visiteduris element.

     
    In your loop, change the following line:
     
            doc.DocumentElement.AppendChild(_usedUris);

    to this:
     
           visiteduris.AppendChild(_usedUris);
     
    then take out the similar line after the close of the loop.

    --
    Mike
    • Marked as answer by Bacanz Sunday, July 4, 2010 10:48 PM
    Sunday, July 4, 2010 10:34 PM

All replies

  • You are appending the _usedUris element outside the visiteduris element.

     
    In your loop, change the following line:
     
            doc.DocumentElement.AppendChild(_usedUris);

    to this:
     
           visiteduris.AppendChild(_usedUris);
     
    then take out the similar line after the close of the loop.

    --
    Mike
    • Marked as answer by Bacanz Sunday, July 4, 2010 10:48 PM
    Sunday, July 4, 2010 10:34 PM
  • Thanks working great :). That's the last thing I would have tried tbh. I thought that would iterate loads of </visited> tags as it's within the loop, care to explain?
    Sunday, July 4, 2010 10:48 PM

  • The DocumentElement is the <websites> element in your case.  You were constantly appending the _useduri elements to this element, when the example you cited had them under the visited element.  Since you only created one visited element you were on the right track.  You were just attaching them to the wrong parent node.

    --
    Mike
    Sunday, July 4, 2010 11:03 PM