XML Question

  • Thread starter Thread starter Michael C
  • Start date Start date
M

Michael C

Hi all,

I'm back with another XML question. We covered this briefly before, but I'm
still having problems with it. Basically what I need to do is be able to
add, delete and update nodes and their attributes in an XML file. Here's a
sample of an XML file I might use:

<root>
<settings>
<setting name="timeout">50</setting>
<setting name="loginattempts">3</setting>
</settings>
<servers>
<server name="sql1" username="sa" password="password"/>
<server name="sql2" username="user" password="upassword"/>
</servers>
</root>

I need to change the values of the <setting> nodes by name and add/delete or
modify the <server> tag attributes. As it stands right now, I read in the
entire file using an XmlTextReader; and when I update it, I re-write the
entire file using a StreamWriter. Can someone post, or direct me to, some
sample code that can get me on the road to properly using my XML file?
Meanwhile, I'll just be hitting the books :)

Thanks in advance,
Michael C.
 
Michael said:
Hi all,

I'm back with another XML question. We covered this briefly before, but
I'm
still having problems with it. Basically what I need to do is be able to
add, delete and update nodes and their attributes in an XML file. Here's
a sample of an XML file I might use:

<root>
<settings>
<setting name="timeout">50</setting>
<setting name="loginattempts">3</setting>
</settings>
<servers>
<server name="sql1" username="sa" password="password"/>
<server name="sql2" username="user" password="upassword"/>
</servers>
</root>

I need to change the values of the <setting> nodes by name and add/delete
or
modify the <server> tag attributes. As it stands right now, I read in the
entire file using an XmlTextReader; and when I update it, I re-write the
entire file using a StreamWriter. Can someone post, or direct me to, some
sample code that can get me on the road to properly using my XML file?
Meanwhile, I'll just be hitting the books :)

Thanks in advance,
Michael C.

You should be able to just load it and save it using those methods of the
XmlDocument class.

XmlDocument xd = new XmlDocument()
xd.Load("MyDoc.xml");
xd.Save("MyDoc.xml");

Now as far as modifying the value, use the innerText property after getting
to the node you want. You can set its value. The attributes are a
collection of the node. You can set those as well, one you've iterated
through them.

Alternatively, create an XPath query to retrieve the node and do the above.
I wrote a Windows service that does some of what you are asking.

Here's some sample code:

using System;
using System.Xml;

namespace xmltest{
class xpathtest1
{
[STAThread]
static void Main(string[] args)
{
if(args.Length < 2)
{
Console.WriteLine("Incorrect number of parameters.");
goto endApp;
}

try
{
//Load the XML document
XmlDocument doc = new XmlDocument();
doc.Load(args[0]);

//Select the nodes
XmlNodeList resultNodes = doc.SelectNodes(args[1]);
Console.WriteLine("{0} matching nodes found...",
resultNodes.Count);

//Print the node values
int iCount = 0;
foreach(XmlNode aNode in resultNodes)
Console.WriteLine("Node {0} value: {1}",
++iCount, aNode.InnerText);
}
catch(Exception exp)
{
Console.WriteLine("Error: " + exp.ToString());
}

endApp:
Console.Write("Press Enter to continue...");
Console.ReadLine();

}
}
}
 

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

Back
Top