Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. writing to xml file

writing to xml file

Scheduled Pinned Locked Moved XML / XSL
xmlhelptutorialquestionannouncement
5 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Farraj
    wrote on last edited by
    #1

    Hi, I need help writing to an xml file. this is the XML file

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <photos>
    <photo filename="5.jpg" thumbnail="11.jpg" description="jime" />
    <photo filename="6.jpg" thumbnail="121.jpg" description="Water Drops (800x600)" />
    <photo filename="8.jpg" thumbnail="22.jpg" description="Fireworks (640x480)" />

    </photos>

    any ideas how to do that? Thanks, Farraj

    S 1 Reply Last reply
    0
    • F Farraj

      Hi, I need help writing to an xml file. this is the XML file

      <?xml version="1.0" encoding="utf-8" standalone="yes"?>
      <photos>
      <photo filename="5.jpg" thumbnail="11.jpg" description="jime" />
      <photo filename="6.jpg" thumbnail="121.jpg" description="Water Drops (800x600)" />
      <photo filename="8.jpg" thumbnail="22.jpg" description="Fireworks (640x480)" />

      </photos>

      any ideas how to do that? Thanks, Farraj

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Hmmm - bit of information lacking, like where are you getting the information you want to write to the XML file, what language are you using, etc. And also - can you clarify what help you actually want….I mean, you've got an XML file there, you've obviously written it somehow…so what's your problem?

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

      F 1 Reply Last reply
      0
      • S Stuart Dootson

        Hmmm - bit of information lacking, like where are you getting the information you want to write to the XML file, what language are you using, etc. And also - can you clarify what help you actually want….I mean, you've got an XML file there, you've obviously written it somehow…so what's your problem?

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

        F Offline
        F Offline
        Farraj
        wrote on last edited by
        #3

        thank you for your reply im using C#. ive tried building such a file using linq

        XDocument doc = XDocument.Load(Server.MapPath(@"file.xml"));

           var temp = (from element in doc.Root.Elements("photo") where element.Attribute("filename").Value == txtBoxFileName.Text && element.Attribute("thumbnail").Value == txtBoxThumbname.Text && element.Attribute("description").Value == txtBoxDesc.Text select element);
        
                XElement result = null;
        
                if (temp.Count() == 0)
                {
                    result = doc.Root.Elements().Last();
                    result.AddAfterSelf(new XElement("photo", new XAttribute("filename", txtBoxFileName.Text), new XAttribute("thumbnail", txtBoxThumbname.Text), new XAttribute("description", txtBoxDesc.Text)));
                    result = doc.Root.Elements().Last();
                }
                else
                {
                    result = temp.First();
                }
               
             
                result.Add(new XElement("photo", new XAttribute("filename", txtBoxFileName.Text), new XAttribute("thumbnail", txtBoxThumbname.Text), new XAttribute("description", txtBoxDesc.Text)));
                
                doc.Save(Server.MapPath(@"file.xml"));
                this.lblxml.Text = "done";
        

        this cool code was made for searching a specific node and add the data inside it. it helps. it adds! but, not the way i need it. i only need to add one node of ((< photo a="1" b="2" c="3" / >)) which i think its pretty simple, im just not good enough at it appreciating any help. thanks, Farraj

        E 1 Reply Last reply
        0
        • F Farraj

          thank you for your reply im using C#. ive tried building such a file using linq

          XDocument doc = XDocument.Load(Server.MapPath(@"file.xml"));

             var temp = (from element in doc.Root.Elements("photo") where element.Attribute("filename").Value == txtBoxFileName.Text && element.Attribute("thumbnail").Value == txtBoxThumbname.Text && element.Attribute("description").Value == txtBoxDesc.Text select element);
          
                  XElement result = null;
          
                  if (temp.Count() == 0)
                  {
                      result = doc.Root.Elements().Last();
                      result.AddAfterSelf(new XElement("photo", new XAttribute("filename", txtBoxFileName.Text), new XAttribute("thumbnail", txtBoxThumbname.Text), new XAttribute("description", txtBoxDesc.Text)));
                      result = doc.Root.Elements().Last();
                  }
                  else
                  {
                      result = temp.First();
                  }
                 
               
                  result.Add(new XElement("photo", new XAttribute("filename", txtBoxFileName.Text), new XAttribute("thumbnail", txtBoxThumbname.Text), new XAttribute("description", txtBoxDesc.Text)));
                  
                  doc.Save(Server.MapPath(@"file.xml"));
                  this.lblxml.Text = "done";
          

          this cool code was made for searching a specific node and add the data inside it. it helps. it adds! but, not the way i need it. i only need to add one node of ((< photo a="1" b="2" c="3" / >)) which i think its pretty simple, im just not good enough at it appreciating any help. thanks, Farraj

          E Offline
          E Offline
          Estys
          wrote on last edited by
          #4

          Don't add the XElement twice :

          result.AddAfterSelf( ... )
          ...
          ...
          result.Add( ... );

          Cheers

          I don't like my signature at all

          F 1 Reply Last reply
          0
          • E Estys

            Don't add the XElement twice :

            result.AddAfterSelf( ... )
            ...
            ...
            result.Add( ... );

            Cheers

            I don't like my signature at all

            F Offline
            F Offline
            Farraj
            wrote on last edited by
            #5

            i tried that also but it opens the last node and inserts the node i wanted and closes the last node that was opened. any idea? i would love if you write me the suitable code. thanks a lot

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups