Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everybody.

I have a file Xml as

XML
<playlist >
 <tracklist>
   <track>
     <title>Dragostea-Sin-Tei</title>
     <annotation>ThanhTungProduction</annotation>
     <location>Upload/Dragostea-Sin-Tei.mp3</location>
    </track>
 </tracklist>
</playlist>


I want to write data to xml file by c#, please help me!! Thanks!
Posted

C#
//Create a new XmlDocument
XmlDocument doc = new XmlDocument();
//Load the xml from the string 
string xmlString="<playlist>........</playlist>";
doc.LoadXml(xmlString);
//Or read from file
doc.Load(inputFileName);
//Make modifications. There are several possibilities.  For example
doc.GetElementsByTagName("title")[0].InnerText+=" Modified";
//Write to a file with name fileName
doc.Save(fileName);
 
Share this answer
 
v2
just go through this link
you can find everything about XML here...

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^]
 
Share this answer
 
v2
//Create xml

private void createxml()
{
DataTable tb = new DataTable();

tb.Columns.Add("title", Type.GetType("System.String"));
tb.Columns.Add("annotation", Type.GetType("System.String"));
tb.Columns.Add("location", Type.GetType("System.String"));

tb.TableName = "track";

DataRow r = tb.NewRow();

r[0] = "My Love";
r[1] = "Westlife";
r[2] = "mylove.mp3";

tb.Rows.Add(r);
String st = Server.MapPath("listAudio.xml");
tb.WriteXml(st,true);
}

Write data to Xml

String st = Server.MapPath("listAudio.xml");
DataSet ds = new DataSet();
ds.ReadXml(st);
DataRow r = ds.Tables[1].NewRow();
r[0] = TextBox1.Text;
r[1] = TextBox2.Text;
r[2] = TextBox3.Text;
ds.Tables[1].Rows.Add(r);

but Result

XML
<playlist >
 <tracklist>
   <track>
     <title>Dragostea-Sin-Tei</title>
     <annotation>ThanhTungProduction</annotation>
     <location>Upload/Dragostea-Sin-Tei.mp3</location>
    </track>
 </tracklist>

<track>
    <title>Only Love</title>
	<annotation>Westlife</annotation>
	<location>Only Love.mp3</location>
  </track>
</playlist>



Please help me!Thanks!
 
Share this answer
 
SQL
Ever think to solve the problem with serialization?
You can read your data from an XML file and write it back into an XML file, as listed in the following example:


C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace PlayList
{
    public partial class Form1 : Form
    {
        private const string SAVEPATH = @"C:\Temp\playlist.xml";
        private PlayList _playList = new PlayList();

        public Form1()
        {
            InitializeComponent();
            Test();
        }

        private void Test()
        {
            CreateDummyList();
            Save(SAVEPATH, _playList);
            PlayList newPlayList = (PlayList)Restore(SAVEPATH, typeof(PlayList));
        }

        private void CreateDummyList()
        {
            _playList.Tracks.Add(new Track("Dragostea-Sin-Tei", "ThanhTungProduction", "Upload/Dragostea-Sin-Tei.mp3"));
            _playList.Tracks.Add(new Track("Yesterday", "Beatles", "Upload/Yesterday.wav"));
            _playList.Tracks.Add(new Track("Devil in disguise", "Elvis Presley", "Upload/Devil in disguise.avi"));
        }

        public static void Save(string path, object obj)
        {
            FileStream outFile = File.Create(path);
            XmlSerializer formatter = new XmlSerializer(obj.GetType());
            formatter.Serialize(outFile, obj);
            outFile.Close();
        }

        public static object Restore(string path, Type type)
        {
            FileStream inFile;
            inFile = File.OpenRead(path);
            XmlSerializer formatter = new XmlSerializer(type);
            byte[] buffer = new byte[inFile.Length];
            inFile.Read(buffer, 0, (int)inFile.Length);
            inFile.Close();
            MemoryStream stream = new MemoryStream(buffer);
            stream.Position = 0;
            return formatter.Deserialize(stream);
        }
    }

    [Serializable]
    // Just to get the class-name in lower case
    [XmlType(TypeName = "playlist")]
    public class PlayList
    {
        private List<track> _tracks = new List<track>();
        // A little bit tricky to get List<track> as <tracklist>-XML-Tag
        [XmlArray(ElementName="tracklist")]
        public List<track> Tracks
        {
            get { return _tracks; }
            set { _tracks = value; }
        }
        
    }

    [Serializable]
    [XmlType(TypeName="track")]
    public class Track
    {
        // Standard constructor is always needed for serialisation
        public Track() { }

        public Track(string title, string annotation, string location)
        {
            _title = title;
            _annotation = annotation;
            _location = location;
        }

        private string _title;
        // Just to get the property-name in lower case
        [XmlElement("title")]
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        private string _annotation;
        [XmlElement("annotation")]
        public string Annotation
        {
            get { return _annotation; }
            set { _annotation = value; }
        }

        private string _location;
        [XmlElement("location")]
        public string Location
        {
            get { return _location; }
            set { _location = value; }
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900