Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple XML writer routine. Need help!

Status
Not open for further replies.

pheadxdll2

Programmer
Jul 1, 2007
1
US
XML has been my crutch for years and I always just want to get it out of the way, regardless of how I do it. :p

I got this simple routine that I wanted to expand upon but wasn't sure how to do it. Long story short, I totally rewrote it and it never worked since. I basically just need it to append to my application's settings xml file. Its basic structure looks like this:

<settings>
<startOnWin>Yes</startOnWin>
<time>h</time>
</settings>

My old routine basically had two different things to do:
1. See if the file exists or not. If it doesn't. Make one with the right structure. (just add <settings> )
2. If the file exists and you want to change, for example <time> up there ^, then change the value by appending to the xml file. If <time> doesn't exist then create it.

As you can see my knowledge for xml terminology isn't that extensive. :) Please give me a break.

If somebody could write me up a little routine that would be awesome. I've pretty much got the entire app written and all I'd have to do is just plug in this code.

Thanks so much,

Alex

 
Welcome to Tek-Tips pheadxdll2.

Take a look at faq732-6677

#2 in particular. The FAQ will help you to understand how the forums work and effective use of them.

For your situation at hand it should be straight forward and not complex. Deffinetely don't make this over complicated as with any development task. it is not much more different than reading and writing to a simple text file. note there are a few different ways of handling XML. For example here is one but then again here is a different. For the writing I'll let you read throught he sevearl links @ This great place. Sorry spent most of the time I have getting the FAQ in :)

To get you going though....

For your conditioning it will simply be a factor of how you for one structure the program. If you a class to handle all of this and call upon its methods you can simple run trhough a basic if..elseif..else statement to figure out where you are in the files existance, element validations and writing or reading to it.

To answer your first question you need to explore the System.IO namespace. This will provide you with the
File.Exists method.

nothing more to this than return a bool such as
Code:
        public static bool ValidateFile()
        {
            return File.Exists(@"Settings.xml");
        }

If the file exists go ahead and read/write or whatever you need to do with it. If it doesn't there is no special method for creating an XML file over any file. Just .Create it ;-) Look into the same File class I pointed to above for the method.

In order to work with your XML file you'll need to laod it into a XmlDocument object.

e.g.
Code:
mlDocument xmldoc = new XmlDocument();
FileStream fs = new FileStream(@"Settings.xml", FileMode.Open, FileAccess.Read,FileShare.ReadWrite);
xmldoc = new XmlDocument();
xmldoc.Load(fs);

Now you have it at your disposal.

What you will need to do is load the nodes into a XmlNodeList. From your example you'll want to load settings. remember XML is case sensitive!

After you have the node list populated you can read several different things by using the members listed through this link. you'll see there are several different things you can go at and or node references.

Hope it gets you going. When you get something together let us know if we can assist if you run into bumps. Also it's nice to post your solutions for future members that may require the same type of task and are having problems with it. Hey, maybe even write an FAQ :)

____________ signature below ______________
You are a amateur developer until you realize all your code sucks.
Jeff Atwood

 
Code:
void AddaNode(ref string xmlString, string nodeName, string value)
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xmlString);
    string xPath = string.Format("//{0}",nodeName);
    XmlNode node = xmlDoc.SelectSingleNode(xPath);
    if (node != null)
    {
        node.InnerXml = value;
    }
    else
    {
        XmlNode newNode = xmlDoc.CreateNode(XmlNodeType.Element, nodeName, null);
        newNode.InnerXml = value;
        xmlDoc.DocumentElement.AppendChild(newNode);
    }
    xmlString = xmlDoc.OuterXml; 
}
where xmlString is the xml to append,
nodeName is the name of the node you want to add/append,
value is the value to put into the node.
O.B
 
Or hey, you could just have someone do your work for you.

Either way hope you actually learn something

____________ signature below ______________
You are a amateur developer until you realize all your code sucks.
Jeff Atwood

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top