PC Review


Reply
Thread Tools Rate Thread

Basic XML file creation / editing

 
 
Sam Collett
Guest
Posts: n/a
 
      18th Mar 2005
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.

 
Reply With Quote
 
 
 
 
Maqsood Ahmed
Guest
Posts: n/a
 
      18th Mar 2005
Hello,
Have a look at System.Xml.XmlDocument class in MSDN, i feel it is much
easier and flexible to use than XmlTextWriter class.
Cheers.

Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Sam Martin
Guest
Posts: n/a
 
      18th Mar 2005
this should help


System.Xml.XmlDocument d = new System.Xml.XmlDocument();

if (System.IO.File.Exists("youxml.xml"))
{
d.Load("youxml.xml");
}
else
{
d.LoadXml("<files></files>");
}

// to add a file

System.Xml.XmlElement fileelem = d.CreateElement("file");

System.Xml.XmlElement text = d.CreateElement("Text");
text.InnerText = "some text";

System.Xml.XmlElement name = d.CreateElement("Name");
text.InnerText = "some name";

fileelem.AppendChild(text);
fileelem.AppendChild(name);

d.DocumentElement.AppendChild(fileelem);

d.Save("youxml.xml");

/*
// now i'm guessing you'll want to check for dupes
// use something like this maybe

foreach(System.Xml.XmlElement elem in d.DocumentElement.ChildNodes)
{
// each elem represents a <file>
}

// if found, don't append the element.
*/

"Sam Collett" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
Sam Collett
Guest
Posts: n/a
 
      18th Mar 2005
Is there any other guide to do this, apart from the MSDN reference?

i.e. Pseudo code:

Load file into XmlDocument.
If file does not exist, create it (with xml decleration, and root
element).
String Text = "My File"
String Name = "MyFile.txt"
SELECT <File> FROM <Files>
If RecordsFound > 0 Then
Boolean TextFound = False
For <File> in <Files>
If <Text> = Text Then TextFound = True
Next
If Not TextFound Then Add New <File> To <Files>
Add <Text> = Text
Add <File> = Name
End If

 
Reply With Quote
 
Landi
Guest
Posts: n/a
 
      18th Mar 2005
Check this out if you don't want to look at MSDN documentation then.
http://dowhileloop.com/publicjoe/cspdf/htp18.pdf
It has a log of examples for what you are trying to do.
Hope it helps.

--
(E-Mail Removed)
http://dowhileloop.com website development
http://publicjoe.dowhileloop.com -- C# Tutorials
"Sam Collett" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Is there any other guide to do this, apart from the MSDN reference?
>
> i.e. Pseudo code:
>
> Load file into XmlDocument.
> If file does not exist, create it (with xml decleration, and root
> element).
> String Text = "My File"
> String Name = "MyFile.txt"
> SELECT <File> FROM <Files>
> If RecordsFound > 0 Then
> Boolean TextFound = False
> For <File> in <Files>
> If <Text> = Text Then TextFound = True
> Next
> If Not TextFound Then Add New <File> To <Files>
> Add <Text> = Text
> Add <File> = Name
> End If
>



 
Reply With Quote
 
Nick Malik [Microsoft]
Guest
Posts: n/a
 
      18th Mar 2005
Hi Sam,

An XML file can be many things, but first and foremost it is simply a format
for storing data and objects. You've been looking at it as a format for
storing data. I'd encourage you to look at it as a format for storing
objects... specifically a DataSet object.

You can create a dataset object, add a table for the data you want, and add
the columns for Text and Name like you've described. You can then add the
data rows that you want, sort it, bind it to a databound control, and have
loads of fun, all using tons of documentation online that assumes you've
retrieved the dataset from a database!

Storing it and reading it is called Serializing and Deserializing the
DataSet. (hint: think MSN Search or Google)

Nearly every operation you've described is considered to be 'basic database
operations' and is therefore completely supported with a dataset object.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Sam Collett" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
Sam Collett
Guest
Posts: n/a
 
      18th Mar 2005
Thanks.
I used ReadXml and WriteXml to get the desired effect. Removes
duplicate entries as well.

private static bool AddFile(string text, string name)
{
bool success = false;
string outfile =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
"files.xml");
DataSet ds = new DataSet("Files");
// file table
DataTable files = new DataTable("File");
// file columns
files.Columns.Add("Text", typeof(string));
files.Columns.Add("Name", typeof(string));
ds.Tables.Add(files);
try {
ds.ReadXml(outfile);
} catch (Exception ex) {
Console.WriteLine(ex.Source + " : " + ex.Message);
}
DataRow[] ExistingRecords;
ExistingRecords = files.Select("Name = '" + name + "'");
// add if not exists
if (ExistingRecords.Length == 0) {
DataRow NewFile;
NewFile = files.NewRow();
NewFile["Text"] = text;
NewFile["Name"] = name;
files.Rows.Add(NewFile);
} else {
// loop through existing records to find duplicates
for (int i=0; i<ExistingRecords.Length; i++) {
// if not the first record, delete
if (i!=0) {
ExistingRecords[i].Delete();
}
}
// change if does exist
ExistingRecords[0]["Text"] = text;
}
try {
ds.WriteXml(outfile);
success = true;
} catch (Exception ex) {
Console.WriteLine(ex.Source + " : " + ex.Message);
//throw(ex);
}
return success;
}

 
Reply With Quote
 
Sam Collett
Guest
Posts: n/a
 
      18th Mar 2005
Nice site you have there. Lots of good tutorials. You have more than a
books worth when you combine all free chapters from various books.

 
Reply With Quote
 
Landi
Guest
Posts: n/a
 
      18th Mar 2005
Thanks Sam, You actually should thank Mike and not me,
I am actually only hosting it for http://www.publicjoe.f9.co.uk
He was asking people for help because he doesn't have enough bandwidth at
certain months so he wants to have mirrors out there. I am more than happy
to do it for him. I actually remembered that I had been at his site a few
years back and getting some very valuable information. Who would of thought
that I would of hosted a mirror of his site. Mike is constantly writing and
posting new articles from time to time. Now that I know a lot more about
..NET I am going to help him out and write some articles for him to post on
the site.
there is also another mirror at http://publicjoe.justbe.com

--
(E-Mail Removed)
http://dowhileloop.com website development
http://publicjoe.dowhileloop.com -- C# Tutorials

"Sam Collett" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Nice site you have there. Lots of good tutorials. You have more than a
> books worth when you combine all free chapters from various books.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Editing the creation date in visual basic =?Utf-8?B?SmVsbHkgQmVsbHk=?= Microsoft Outlook Discussion 4 20th Aug 2007 10:02 PM
Editing files- need to disable links from file creation =?Utf-8?B?YmVhY2hnaXJsMw==?= Microsoft Outlook Contacts 3 18th May 2007 06:54 PM
Ftp on IE 7 basic issues. -folder creation/etc Martin Vaupell Windows Vista General Discussion 0 19th Apr 2007 10:33 PM
restricting group policy editing and creation barabba72@hotmail.com Microsoft Windows 2000 Group Policy 2 21st Aug 2005 02:59 AM
TOA - creation/editing =?Utf-8?B?U2NvdHQgVHVyY2hpbg==?= Microsoft Word Document Management 4 14th Oct 2004 07:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:24 AM.