how to write to an xml file

A

ALI-R

I have tree variable with their values:

1) rptName="report1"
2) rptPath="c:\......"
3) serverPath=http://server...
and
4)an arry which contain Name/value pairs.

what is the best way of wrting them into an xml file??
these Xml files are stored in the same directory so I also give the XML file
a uniqe name so I can make sure that it dosn't overrite the other file.


thanks for your help.
ALI
 
H

Hugo Wetterberg

Hi Ali,
Create an object representation of the xml file and fill it with data:

object NameValue
{
string Name;
string Value;
}

object Settings
{
string ReportName;
string ReportPath;
string ServerPath;
NameValue Pairs;
}

Settings settingsObject=new Settings();
settingsObject.ReportName="My report";
settingsObject.ReportPath="c:\\...";
settingsObject.ServerName="http://server...";

Then use XmlSerializer in the System.Xml.Serialization namespace and
serialize the object to disk:

FileStream fs=File.Create("mysettings.xml");
try
{
XmlSerializer serializer=new XmlSerializer(typeof(Settings));
serializer.Serialize(fs,settingsObject);
}
finally
{
fs.Close();
}

There you go

/Hugo
 
W

William Stacey

I would just create a class and use xmlseralizer to serialize it to xml.\
pubic class MyClass
{
public string RptName;
public string RptPath;
pubic DictionaryEntry[] Values;

public string ToXmlString()
{
// use XmlSerializer ...
}
public static MyClass FromXmlString()
{
// use XmlSerializer. Not at my computer to give you better.
}
}
 
A

ALI-R

Is the second method ( FromXmlString) for Deserilizing??
William Stacey said:
I would just create a class and use xmlseralizer to serialize it to xml.\
pubic class MyClass
{
public string RptName;
public string RptPath;
pubic DictionaryEntry[] Values;

public string ToXmlString()
{
// use XmlSerializer ...
}
public static MyClass FromXmlString()
{
// use XmlSerializer. Not at my computer to give you better.
}
}

ALI-R said:
I have tree variable with their values:

1) rptName="report1"
2) rptPath="c:\......"
3) serverPath=http://server...
and
4)an arry which contain Name/value pairs.

what is the best way of wrting them into an xml file??
these Xml files are stored in the same directory so I also give the XML
file
a uniqe name so I can make sure that it dosn't overrite the other file.


thanks for your help.
ALI
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top