Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 28th, 2006, 11:25 PM
Todd Price
Guest
 
Posts: n/a
Default Replace Empty Node

I'm trying to use XPath to get a handle on one specific node so that I can
replace it with an xml fragment I've loaded from another file.

So if I have this XML:

<issue>
<id>1</id>
<date>7/28/2006</date>
<states/>
</issue>

If "myDoc" is the preceding XML loaded into an XmlDocument object, then this
code is what I want to return the <states/node above, but it returns null
instead:

XmlNode statesNode = doc.SelectSingleNode("//states");

I can't seem to get this XPath to return a node. Can someone help me out
here? Is there a better approach?
  #2  
Old August 1st, 2006, 07:55 PM
Ramya A
Guest
 
Posts: n/a
Default Re: Replace Empty Node

You need a XMLNameSpaceManager before you use SelectSingleNode

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace ("IL","urn:IssueList");
xmlNode = xmlDoc.SelectSingleNode("issue/states", nsmgr);

Also, if this does not return an empty string, then try including
<states></states>
instead of <states/>

Hope this helps
Ramya
Todd Price wrote:
Quote:
I'm trying to use XPath to get a handle on one specific node so that I can
replace it with an xml fragment I've loaded from another file.
>
So if I have this XML:
>
<issue>
<id>1</id>
<date>7/28/2006</date>
<states/>
</issue>
>
If "myDoc" is the preceding XML loaded into an XmlDocument object, then this
code is what I want to return the <states/node above, but it returns null
instead:
>
XmlNode statesNode = doc.SelectSingleNode("//states");
>
I can't seem to get this XPath to return a node. Can someone help me out
here? Is there a better approach?
  #3  
Old August 1st, 2006, 09:35 PM
Todd Price
Guest
 
Posts: n/a
Default Re: Replace Empty Node

That was it! I had been having complications with another issue due to
namespaces, and this cleared that up as well. Thanks!

"Ramya A" wrote:
Quote:
You need a XMLNameSpaceManager before you use SelectSingleNode
>
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace ("IL","urn:IssueList");
xmlNode = xmlDoc.SelectSingleNode("issue/states", nsmgr);
>
Also, if this does not return an empty string, then try including
<states></states>
instead of <states/>
>
Hope this helps
Ramya
Todd Price wrote:
Quote:
I'm trying to use XPath to get a handle on one specific node so that I can
replace it with an xml fragment I've loaded from another file.

So if I have this XML:

<issue>
<id>1</id>
<date>7/28/2006</date>
<states/>
</issue>

If "myDoc" is the preceding XML loaded into an XmlDocument object, then this
code is what I want to return the <states/node above, but it returns null
instead:

XmlNode statesNode = doc.SelectSingleNode("//states");

I can't seem to get this XPath to return a node. Can someone help me out
here? Is there a better approach?
>
>
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles