writing to xml file
-
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
-
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
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!
-
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!
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
-
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
-
Don't add the
XElement
twice :result.AddAfterSelf( ... )
...
...
result.Add( ... );Cheers
I don't like my signature at all