all groups > dotnet xml > april 2004 >
You're in the

dotnet xml

group:

Newbie xpath Question


Newbie xpath Question cab0san NO[at]SPAM hotmail.com
4/28/2004 10:40:47 PM
dotnet xml: I'm new to this, so I've been reading about namespaces and xpath
queries.

I am trying to write a dotnet program that will change the value of a
node. I am getting the file from another vendor, so I can't control
the format.

Here's a sample

============================================================

<?xml version="1.0" encoding="utf-8"?>
<catalog xmlns="http://someserver/somewhere"
xmlns:ab="http://anotherserver/rightthere">
<cd country="USA">
<title>Reason</title>
<artist>Capital 8</artist>
<price>9.99</price>
</cd>
<cd country="USA">
<title>Yoshimi Battles The Pink Robots</title>
<artist>The Flaming Lips</artist>
<price>15.99</price>
</cd>
<cd country="USA">
<title>Nugget</title>
<artist>Cake</artist>
<price>12.99</price>
</cd>
<cd country="USA">
<title>Under The Cold Blue Stars</title>
<artist>Josh Rouse</artist>
<price>13.00</price>
</cd>
</catalog>

============================================================

Dim doc As New System.Xml.XmlDocument
Dim fName As String = "c:\temp\test.xml"
doc.Load(fName)
Dim ns As New XmlNamespaceManager(doc.NameTable)
ns.AddNamespace("ns", "xmlns=""http://someserver/somewhere""")
' (also tried ns.AddNamespace("",
xmlns=""http://someserver/somewhere""")
ns.AddNamespace("ab", "xmlns:ab=""http://anotherserver/rightthere""")

Dim xnode As XmlNode

' Let's say I want to change the price of the first cd

xnode = doc.SelectSingleNode("/ns:catalog/cd/price", ns)

xnode.InnerXml = "1"

doc.save(fName + ".new.xml")

SelectSingleNode never seems to work unless I ignore namespaces
(using an xmltextreader.namespaces=false).

Re: Newbie xpath Question Yan Leshinsky
4/28/2004 10:54:44 PM
Try
xnode = doc.SelectSingleNode("/ns:catalog/ns:cd/ns:price", ns)


[quoted text, click to view]

Re: Newbie xpath Question cab0san NO[at]SPAM hotmail.com
4/29/2004 5:44:18 AM
That didn't work. xnode is still Nothing.

[quoted text, click to view]
Re: Newbie xpath Question Tejal Joshi (MSFT)
4/29/2004 10:09:28 AM
Change
ns.AddNamespace("ns", "xmlns=""http://someserver/somewhere""")
to
ns.AddNamespace("ns", "http://someserver/somewhere")

the "xmlns=" is Not part of the URI, the prefix "ns" should map to
"http://someserver/somewhere"

--
This posting is provided "AS IS" with no warranties, and confers no rights.

[quoted text, click to view]

Re: Newbie xpath Question cab0san NO[at]SPAM hotmail.com
4/29/2004 1:35:54 PM
I got it.

The .addNamespace method arguments were specified incorrectly.

they were...


ns.AddNamespace("ns", "xmlns=""http://someserver/somewhere""")
ns.AddNamespace("ab", "xmlns:ab=""http://anotherserver/rightthere""")

they should be...

ns.AddNamespace("ns", "http://someserver/somewhere")
Re: Newbie xpath Question cab0san NO[at]SPAM hotmail.com
4/29/2004 4:46:21 PM
Thank you!

[quoted text, click to view]
Re: Newbie xpath Question Oleg Tkachenko [MVP]
4/29/2004 7:00:45 PM
[quoted text, click to view]

Second argument is namespace uri. Use

ns.AddNamespace("ns", "http://someserver/somewhere")

--
Oleg Tkachenko [XML MVP, XmlInsider]
AddThis Social Bookmark Button