471,353 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

XML Processing and c#

Hi all, just looking for a little guidance with xml procerssing.

Imagine the scenario, I have a XmlDocument object populated with XML. I need
to find and replaces some of the text in one of the nodes, i.e. the basic
XML below, I want to substitute _replace_with_value_ with some data,

<root>
....
<tag>_replace_with_value_</tag>
....
</root>

What is the process for doing this. I can find the node using
SelectSingleNode, however this extracts it into a new NodeElement, what do I
need todo to perform a simple search and replace on my main document object?

Thanks,

Kind regards
Taz
Dec 2 '05 #1
6 1453
SelectSingleNode does not extract the node into a new XmlNode. It
returns the node from the document that matched the selection criteria.

So, if you say:

XmlNode tagNode = doc.SelectSingleNode("root/tag");
tagNode.InnerText = "changed";

then the contents of the tag within the XML document doc will change to
"changed". tagNode is a reference into the XmlDocument, not a copy.

Dec 2 '05 #2
Many thanks Bruce, problem solved. However a further question to increase my
understanding, why does a namespace object have to be given to the
SelectSingleNode member before executing?

i.e.

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("xsi", http://www.w3.org/2001/XMLSchema-instance);

what does this achieve?

Thanks again,
Taz
Dec 2 '05 #3
Namespaces in XML server pretty much the same purpose as they do in .NET
Framework. They serve to disambiguate between element tag names that may be
the same but belong to different groups. That's the short answer, if you
really wnat to get farther into XML the Quickstarts has some samples and MSDN
Online has extensive documentation.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Tarun Mistry" wrote:
Many thanks Bruce, problem solved. However a further question to increase my
understanding, why does a namespace object have to be given to the
SelectSingleNode member before executing?

i.e.

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("xsi", http://www.w3.org/2001/XMLSchema-instance);

what does this achieve?

Thanks again,
Taz

Dec 2 '05 #4
Hi,
this might be useful in some scenarios like when you have a new child with
which you want to replace with your old child node. as well, it will help
you to replace a old child in a document with new child from some other
document,

XmlNode node = doc.SelectSingleNode("//root/firstchild");

XmlNode node1 = doc.SelectSingleNode("//root/secondchild");

node.ParentNode.ReplaceChild(node1,node);

If you have a new child element - node1 is from other document then

node.ParentNode.ReplaceChild(doc..ImportNode(node1 ,true), ,node);

HTH,

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
SelectSingleNode does not extract the node into a new XmlNode. It
returns the node from the document that matched the selection criteria.

So, if you say:

XmlNode tagNode = doc.SelectSingleNode("root/tag");
tagNode.InnerText = "changed";

then the contents of the tag within the XML document doc will change to
"changed". tagNode is a reference into the XmlDocument, not a copy.

Dec 3 '05 #5
And it is only required when you are using some elements with some prefix
like myns:mynode then myns must be tied with some namespace so you have to
use namespace in that scenario only else no namespace is required and its
only purpose is to distinguish the elements with the same name [if it is
there] from different namespaces like what Namespace in .Net serves the
purpose....
Agree with Peter,

HTH,
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:2F**********************************@microsof t.com...
Namespaces in XML server pretty much the same purpose as they do in .NET
Framework. They serve to disambiguate between element tag names that may be the same but belong to different groups. That's the short answer, if you
really wnat to get farther into XML the Quickstarts has some samples and MSDN Online has extensive documentation.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Tarun Mistry" wrote:
Many thanks Bruce, problem solved. However a further question to increase my understanding, why does a namespace object have to be given to the
SelectSingleNode member before executing?

i.e.

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("xsi", http://www.w3.org/2001/XMLSchema-instance);

what does this achieve?

Thanks again,
Taz

Dec 3 '05 #6
Many thanks everyone!

I really appreciate your help and input.

Everything is much clearer now.

Kind regards
Tarun
Dec 3 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by gl | last post: by
2 posts views Thread by Luiz Vianna | last post: by
4 posts views Thread by Fao, Sean | last post: by
10 posts views Thread by Enrique Cruiz | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.