473,320 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Creating a xml from scratch in C#

Kim
Im making a config file to a C# program in XML. I have save and load
functions, but these require a base XML file with its structure to
exsist (Im new to XML, so this was the way I it to work).
Now I want to extend that, so if the base XML file doesnt exsist a
function will make the structure. However this way seem somewhat
troublesome to do it, but it works.

Is there an easier way?
Also is it allowed to use space in the element name ?

C# code:
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
System.Xml.XmlNode root;
System.Xml.XmlNode node;
System.Xml.XmlNode child, child2, child3;

root = xDoc.CreateElement("Configuration");
xDoc.AppendChild(root);

node = xDoc.CreateElement("Application");
child = xDoc.CreateElement("Name");
child.InnerText = "some value";
node.AppendChild(child);
child = xDoc.CreateElement("Version");
child.InnerText = "some value";
node.AppendChild(child);
child = xDoc.CreateElement("Author");
child.InnerText = "some value";
node.AppendChild(child);
root.AppendChild(node);

node = xDoc.CreateElement("Options");
child = xDoc.CreateElement("Main");
child2 = xDoc.CreateElement("BackupDir");
child.AppendChild(child2);
node.AppendChild(child);

child = xDoc.CreateElement("Settings");
child2 = xDoc.CreateElement("SourceFolders");
child3 = xDoc.CreateElement("Folder");
child3.InnerText = string.Empty;
child2.AppendChild(child3);
child.AppendChild(child2);
child2 = xDoc.CreateElement("Filters");
child3 = xDoc.CreateElement("Filter");
child3.InnerText = string.Empty;
child2.AppendChild(child3);
child.AppendChild(child2);
child2 = xDoc.CreateElement("Recursive");
child.AppendChild(child2);
child2 = xDoc.CreateElement("Overwrite");
child.AppendChild(child2);
child2 = xDoc.CreateElement("CheckForFreeSpace");
child.AppendChild(child2);
node.AppendChild(child);
root.AppendChild(node);

xDoc.Save(filename); //filename is a string passed to this
function with a filename and its extension

This gives an XML as listed below:
<Configuration>
<Application>
<Name>some value</Name>
<Version>some value</Version>
<Author>some value</Author>
</Application>
<Options>
<Main>
<BackupDir />
</Main>
<Settings>
<SourceFolders>
<Folder // This should be on 1 line as <Folder></Folder>
</Folder //
</SourceFolders>
<Filters>
<Filter // This should be on 1 line as <Filter></Filter>
</Filter //
</Filters>
<Recursive / // This should be as <Recursive></Recursive>
<Overwrite / // This should be as <Overwrite></Overwrite>
<CheckForFreeSpace / // This should be as
<CheckForFreeSpace></CheckForFreeSpace>
</Settings>
</Options>
</Configuration>
I need to add at least 1 child on both <SourceFoldersand <Filters>,
or the save/load functions dont work.

Aug 21 '06 #1
3 6087
"Also is it allowed to use space in the element name ?"

see: http://www.w3.org/TR/2000/REC-xml-20001006#dt-stag

And follow the links.

Dickster

Aug 21 '06 #2
Kim
I dont understand any of that.

dickster wrote:
see: http://www.w3.org/TR/2000/REC-xml-20001006#dt-stag

And follow the links.
Aug 22 '06 #3
Could you please elaborate what you mean by "...save/load don't work"? In
XML world

<Folder>
</Folder>
is same as
<Folder></Folder>

child2 = xDoc.CreateElement("Recursive"); // XML will look like
<Recursive>
child2.InnerText = string.Empty; //
<Recursive />

"Kim" <ki*****@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
>I dont understand any of that.

dickster wrote:
>see: http://www.w3.org/TR/2000/REC-xml-20001006#dt-stag

And follow the links.

Aug 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Kennedy | last post by:
How do create a dataset from scratch based on an XML schema. This will not be filled by a dataadapter. It will be a dataset which I add rows to and then save as an XML file. All the example depend...
0
by: Patrick Blackman | last post by:
Hi have created a richtextbox from scratch using the following code: public class TextPad:Control { public TextPad(){} //RichTextBox overrides protected override CreateParams CreateParams {...
4
by: Alvo von Cossel I | last post by:
hi, i have been asked to make a good-looking app for a friend. i have an options form with a big tabstrip in it. 1. how can i customize it e.g. change from the standard system style...
0
by: Chris | last post by:
Ok, I think this is an easy one - I've just been staring at it too long. I'm creating a datagrid in asp.net/c# from scratch, i.e. nothing at all in the aspx page. I'm also creating all the...
1
by: Franck | last post by:
Hello, I need to implement the following features: I need to create a document on whatever format word, pdf … A simple one; few field taken from a DB. This document should be printed on a client...
15
by: guy | last post by:
Where can i find an example of authoring a combobox control FROM SCRATCH in vb.net ??
6
by: André Nogueira | last post by:
Hi there. First of all, sorry for the double post, but I was not sure about which was the most correct group for this question... I am trying to create my own control that will be alot like the...
4
by: crescent_au | last post by:
Hi all, I'm doing some research online on creating php-based multi-level marketing (MLM) system. It seems like a complicated system to build as I need to create one from scratch. I'd like to...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
4
Claus Mygind
by: Claus Mygind | last post by:
I have a question on using the DOM method - But first let me describe what I am doing. 1. I create on all my forms a generic <div id='scratchPad'> </div> 2. I reuse the scratch pad to display...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.