467,894 Members | 1,741 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,894 developers. It's quick & easy.

Basic XML File Creation / Editing

Is there a basic guide on Xml document creation and editing (simpler
than the MSDN docs). Say I want to create a file containing the
following:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Files>
<File>
<Text>Test</Text>
<Name>Test.html</Name>
</File>
</Files>
I know how you can use XMLTextWriter to do this:

XmlTextWriter xtw = new XmlTextWriter("myfile.xml",
System.Text.Encoding.UTF8);
xtw.WriteStartDocument(true);
xtw.WriteStartElement("Files");
xtw.WriteStartElement("File");
xtw.WriteElementString("Text", txtName.Text);
xtw.WriteElementString("Name", FileName);
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndDocument();
xtw.Close();

However, how do you check if the file exists, and append to it instead
of overwriting?

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Files>
<File>
<Text>Test</Text>
<Name>Test.html</Name>
</File>
<File>
<Text>Test 2</Text>
<Name>Test2.html</Name>
</File>
</Files>

Also, I may wish to delete an existing entry, under certain
circumstances (file deleted from disk):

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Files>
<File>
<Text>Test 2</Text>
<Name>Test2.html</Name>
</File>
</Files>

And also, prevent duplicate entries being added (re uploading a file).

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Files>
<File>
<Text>Test</Text>
<Name>Test.html</Name>
</File>
<File>
<Text>Test 2</Text>
<Name>Test2.html</Name>
</File>
<File>
<Text>Test</Text>
<Name>Test.html</Name>
</File>
</Files>

I know there is a class library at the code project that writes to XML
files (http://www.codeproject.com/csharp/ReadWriteXmlIni.asp), but it
does far more than I need and I would rather learn how to do it myself
than rely on a third party library.

I would also like to read the xml from the file and then display in a
page (links to documents, Text is the link text, Name is the file
name). Bound to a repeater, also displaying file size by querying the
file system.

Nov 12 '05 #1
  • viewed: 1473
Share:
1 Reply
you mean like netscape 1992
with document.write in the browser ?

===========================

NO !
Just look at the tripple verbosity

of the object "XmlTextWriter"




<sa*********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Is there a basic guide on Xml document creation and editing (simpler
than the MSDN docs). Say I want to create a file containing the
following:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Files>
<File>
<Text>Test</Text>
<Name>Test.html</Name>
</File>
</Files>
I know how you can use XMLTextWriter to do this:

XmlTextWriter xtw = new XmlTextWriter("myfile.xml",
System.Text.Encoding.UTF8);
xtw.WriteStartDocument(true);
xtw.WriteStartElement("Files");
xtw.WriteStartElement("File");
xtw.WriteElementString("Text", txtName.Text);
xtw.WriteElementString("Name", FileName);
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndDocument();
xtw.Close();

However, how do you check if the file exists, and append to it instead
of overwriting?

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Files>
<File>
<Text>Test</Text>
<Name>Test.html</Name>
</File>
<File>
<Text>Test 2</Text>
<Name>Test2.html</Name>
</File>
</Files>

Also, I may wish to delete an existing entry, under certain
circumstances (file deleted from disk):

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Files>
<File>
<Text>Test 2</Text>
<Name>Test2.html</Name>
</File>
</Files>

And also, prevent duplicate entries being added (re uploading a file).

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Files>
<File>
<Text>Test</Text>
<Name>Test.html</Name>
</File>
<File>
<Text>Test 2</Text>
<Name>Test2.html</Name>
</File>
<File>
<Text>Test</Text>
<Name>Test.html</Name>
</File>
</Files>

I know there is a class library at the code project that writes to XML
files (http://www.codeproject.com/csharp/ReadWriteXmlIni.asp), but it
does far more than I need and I would rather learn how to do it myself
than rely on a third party library.

I would also like to read the xml from the file and then display in a
page (links to documents, Text is the link text, Name is the file
name). Bound to a repeater, also displaying file size by querying the
file system.


Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by SK | last post: by
8 posts views Thread by Sam Collett | last post: by
8 posts views Thread by Eddie Suey | last post: by
5 posts views Thread by Cameron Laird | last post: by
reply views Thread by Hurricane | last post: by
Atli
6 posts views Thread by Atli | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.