XML Question

M

Michael C

Hi all,

Please excuse all the questions... I come from a VB 6 background and am
trying to teach myself C#.NET. I appreciate all your help. My question now
is this -- I have a simple XML file like this:

<?xml version='1.0'?>
<enterprise>
<config>
<load value1="10" value2="14" />
</config>
<servers>
<server name="sqlsrv1" username="" pwd="" integratedsec="yes">
<server name="sqlsrv2" username="sa" pwd="sa" integratedsec="no">
</servers>
</enterprise>

I'm able to read in the information using an XmlTextReader, and when I
update the information I have to re-write the entire file again from
scratch. Just looking at my code I gotta believe there's an easier way...
as it stands I'd be better off with a plain text file :(. Can someone point
me in the right direction for info on efficient uses of the System.Xml
namespace (for instance, to extract certain tags and their attributes or to
add/modify tags or their attributes)? So far I've gone through Microsoft's
examples but I'm still not getting it. Thanks!

Michael C.
 
C

clintonG

M

Mark Broadbent

Hi Michael, nice to see someone similar to me on this ng (I am successfully
forgetting VB it is now just a distant nightmare)

Anyway, I am currently using book 0-7897-2824-9 (QUE Kalani) and so far I
can recommend it for this kind of thing (although doesnt look like you can
grab the code samples ..means typing for a change and actually doing the
exercises ;)

Onto your problem, you are correct that using XmlTextReader you cant write
the elements attributes seperately.
You are also correct that there is an easier way - You need to open an
XmlDataDocument and from this document you can return a reference to a
XmlNode. Using this you can read write.

See this code snippet that I typed in yesterday (you can use it to see how
to use the XmlDataDocument object.

private void buttonReadXML_Click(object sender, System.EventArgs e)

{

XmlTextReader xtr = new XmlTextReader(@"..\..\Books.xml");

xtr.WhitespaceHandling = WhitespaceHandling.None;

XmlDocument xd = new XmlDocument();

xd.Load(xtr);

XmlNode xnodRoot = xd.DocumentElement;

XmlNode xnodWorking;

if (xnodRoot.HasChildNodes)

{

xnodWorking = xnodRoot.FirstChild;

while (xnodWorking != null)

{

addChildren(xnodWorking, 0);

xnodWorking = xnodWorking.NextSibling;

}

}

xtr.Close();

}

private void addChildren(XmlNode xnod, int depth)

{

StringBuilder sbNode = new StringBuilder();

if ( (xnod.NodeType == XmlNodeType.Element) || (xnod.NodeType ==
XmlNodeType.Text) )

{

sbNode.Length = 0;

for (int i = 1; i <= depth; i++)

{

sbNode.Append(" ");

}

sbNode.Append(xnod.Name + " ");

sbNode.Append(xnod.NodeType.ToString());

sbNode.Append(": " + xnod.Value);

this.listBoxNodes.Items.Add(sbNode.ToString());

XmlAttributeCollection atts = xnod.Attributes;

if (atts != null)

{

for (int i = 0; i < atts.Count; i++)

{

sbNode.Length = 0;

for (int j = 1; j <= depth + 1; j++)

{

sbNode.Append(" ");

}

sbNode.Append(atts.Name + " ");

sbNode.Append(atts.NodeType.ToString());

sbNode.Append(": " + atts.Value);

this.listBoxNodes.Items.Add(sbNode);

}

}

XmlNode xnodworking;

if (xnod.HasChildNodes)

{

xnodworking = xnod.FirstChild;

while (xnodworking != null)

{

addChildren( xnodworking, depth + 1);

xnodworking = xnodworking.NextSibling;

}

}

}

}



--


Br,
Mark Broadbent
mcdba , mcse+i
=============
 

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

Top