By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,896 Members | 2,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,896 IT Pros & Developers. It's quick & easy.

Replacing XML Elements programmatically with either VB.Net or C#

P: n/a

Hello and God Bless,

I am stumped trying to get a simple xml database replacing certain data
with other data programmatically. This is what my xml looks like.

****************************************
<Root_Element>
<Topic index="1">
<Sub_Topic>
<Notes></Notes>
<Notes></Notes>
</Sub_Topic>
</Topic>
<Topic index="2">
<Sub_Topic>
<Notes></Notes>
<Notes></Notes>
<Notes></Notes>
</Sub_Topic>
<Sub_Topic>
<Notes></Notes>
</Sub_Topic>
</Topic>
<Topic index="3">
<Sub_Topic>
<Notes></Notes>
</Sub_Topic>
</Topic>
</Root_Element>
****************************************

What i want to do is navigate to <Topic index="2">, and replace all
child nodes under it with new nodes.

Keep in mind that the number of nodes replacing the current ones will be
different. For instance, i wll replace the 2 <Sub_Topicsunder <Topic
index="2"with 4 <Sub_Topicswith different <Notesunder them.

I can read both C# and VB.Net, so it doesnt matter what language you
write in. Please help me.

Thank you all, and God Bless.
*** Sent via Developersdex http://www.developersdex.com ***
Sep 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
XmlDocument xdoc = new XmlDocument();
string strXML = "<Root_Element><Topic
index=\"1\"><Sub_Topic><Notes>";
strXML += "</Notes><Notes></Notes></Sub_Topic></Topic>";
strXML += "<Topic index=\"2\"><Sub_Topic><Notes></Notes>";
strXML += "<Notes></Notes><Notes></Notes></Sub_Topic><Sub_Topic>";
strXML += "<Notes></Notes></Sub_Topic></Topic><Topic index=\"3\">";
strXML +=
"<Sub_Topic><Notes></Notes></Sub_Topic></Topic></Root_Element>";
xdoc.LoadXml(strXML);
while(xdoc.HasChildNodes)
{
for(int i = 0; i < xdoc.SelectNodes("/Root_Element/").Count; i++)
{
if(xdoc.GetElementsByTagName("Topic")[i].Attributes[0].Value.ToString().Equals("2"))
{
/* Now Replace the childNodes */
}
}
}

Sep 28 '06 #2

P: n/a
This code is for retrieval:

XmlDocument xdoc = new XmlDocument();
string strXML = "<Root_Element><Topic
index=\"1\"><Sub_Topic><Notes>";
strXML += "</Notes><Notes></Notes></Sub_Topic></Topic>";
strXML += "<Topic index=\"2\"><Sub_Topic><Notes></Notes>";
strXML += "<Notes></Notes><Notes></Notes></Sub_Topic><Sub_Topic>";
strXML += "<Notes></Notes></Sub_Topic></Topic><Topic index=\"3\">";
strXML +=
"<Sub_Topic><Notes></Notes></Sub_Topic></Topic></Root_Element>";
xdoc.LoadXml(strXML);
while(xdoc.HasChildNodes)
{
for(int i = 0; i < xdoc.SelectNodes("/Root_Element/").Count; i++)
{
if(xdoc.GetElementsByTagName("Topic")[i].Attributes[0].Value.ToString().Equals("2"))
{
/* Now Replace the childNodes */
}
}
}

Sep 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.