XML file

R

R.A.F.

Hi,

I would like to know if there is a particular method to create a blank
XML file, with only following lines:

<?xml version="1.0" encoding="utf-8"?>
<Settings>
</Settings>

where <Settings> is the root node.

thx.

RAF
 
M

Marc Gravell

Well, if it is that simple you could use just the literal? You can do
as follows, but this will self-close the settings node:

using (XmlWriter x = XmlWriter.Create(@"d:\tmp.xml"))
{
x.WriteStartDocument();
x.WriteStartElement("Settings");
x.WriteEndElement();
x.Close();
}

Marc
 
J

Jon Skeet [C# MVP]

R.A.F. said:
I would like to know if there is a particular method to create a blank
XML file, with only following lines:

<?xml version="1.0" encoding="utf-8"?>
<Settings>
</Settings>

where <Settings> is the root node.

Well, this will do it:

using System;
using System.Xml;

class Test
{
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration
("1.0", "utf-8", null));
XmlElement element = doc.CreateElement("Settings");
element.IsEmpty = false;
doc.AppendChild(element);
doc.Save("settings.xml");
}
}

Is that sufficient for you?
 
J

Jon Skeet [C# MVP]

Kerem Gümrükcü said:
Please use your real name!

While I personally prefer it when people use their real names, I don't
really have anything against pseudonyms. I certainly don't think it's
worth telling people off about.
 
R

R.A.F.

sorry but RAF are the first letters of my real name
R.A.F :)

as it is quite long i prefer to make it shorted like that.
 
R

R.A.F.

thx Marc,

this is what i found but i was looking for something better formatted.
i mean that i have the following method :

public static bool CreateXMLEmptyFile(string FileName, string XMLRoot)
{
XmlTextWriter myXMLFileWriter = new XmlTextWriter(@"" + FileName
+ "", Encoding.UTF8);
myXMLFileWriter.Namespaces = false;
myXMLFileWriter.WriteStartDocument();
myXMLFileWriter.WriteStartElement(XMLRoot);
myXMLFileWriter.WriteEndElement();
myXMLFileWriter.WriteEndDocument();
myXMLFileWriter.Close();
return true;
}

this do absolutly the what i need but the XML content formating is not
great. i was expecting something like that :

<?xml version="1.0" encoding="utf-8"?>
<Settings>
</Settings>

and instead of that, i got this :
<?xml version="1.0" encoding="utf-8"?><Settings />

is there something i can do to improve the format of my XML file ?
thx

R.A.F
 
N

Nicholas Paldino [.NET/C# MVP]

I hate to point out the obvious, but why not just do this:

string xml = "<?xml version="1.0"
encoding=\"utf-8\"?><Settings></Settings>";

And then just save that to a file?
 
M

Marc Gravell

NP> I hate to point out the obvious, but...
yup -first thing that occurred to me too (see post)

RAF> i was looking for something better formatted
I realise that you're sorted, but for completeness there is an
XmlWriterSettings object that controls this type of thing.

Marc
 
F

Family Tree Mike

I didn't see anyone mention:

string theXmlString = "<?xml version=\"1.0\"
encoding=\"utf-8\"?>\n<Settings/>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(theXmlString);
 
A

Arne Vajhøj

Kerem said:
Please use your real name!

It happen frequently that people use usenet under initials
or other forms for anonymity.

Not a problem (as long as they do not behave in such a bad way
that it becomes relevant to know who they really are).

Arne
 
M

Marc Gravell

I didn't see anyone mention:
Because that won't create a file - it will just load a DOM. Yes you
could call Save(), but if you're going down the literal route, then
the DOM is unnecessary - just use:
File.WriteAllText(path, "<?xml......>");
which both Nicholas any myself hinted at.

Marc
 
R

R.A.F.

Hi Nicholas,

i do not do that, because those XML files are built in several steps.
moreover, such a string is good, but once i will need to use the other
XML methods and it's better (from my point of view :) to standardize my
process.

RAF
 
F

Family Tree Mike

Marc Gravell said:
Because that won't create a file - it will just load a DOM. Yes you
could call Save(), but if you're going down the literal route, then
the DOM is unnecessary - just use:
File.WriteAllText(path, "<?xml......>");
which both Nicholas any myself hinted at.

Marc

You are right of course. I had perhaps wrongly assumed the poster really
wanted to do further work with the xml document. If so, this goes straight
to a usable document before saving. If they just want a text file/xml file,
the other methods are fine.
 

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