Can I use XPath to write to an xml file?

ucchris77

Member
Joined
Jul 20, 2010
Messages
6
Programming Experience
Beginner
I'm using an xml doc to hold configuration information for my program. Basically what I want to do is navigate through an xml file and change a single value. I found XPath to be much easier to use than XMLReader. So I was wondering if I could use it instead of XMLwriter as well. I am using .Net 2.0 right now so unfortunately LINQ isn't an option.

Well I tried the below code, but ran into an NotSupportedException:

myString = nav.SelectSingleNode(xpath).SetValue("My Value")
 
Not if you're using XPathDocument (re last thread), that is readonly. Load a XmlDocument instead, you can do Xpath selections with it and change the document/nodes/values and write back to xml file.
 
Still having trouble

I changed from using an XPathDocument to an XmlDocument like you said. I also used InnerXml instead of setValue. This code should change the value of the Goal element in the Absenteeism chart from 0.5 to 0.6. The myString variable is just to check when I run it through the debugger. Now when I run it through the debugger the myString variable does change from 0.5 to 0.6 indicating that it worked. The code itself runs fine. But when I open up the actual Xml file the value of Goal is 0.5. It doesn't seem to be saving the value. :confused:

I tried document.save, but that gave me an error which said the file was in use by another process.:mad:

I apologize for not posting for a while, I've been trying to figure this out.

VB.NET:
Dim chartName As String = "Absenteeism"
        Dim elementName As String = "Goal"

        Dim document As New Xml.XmlDocument
        document.Load("C:\Documents and Settings\honeymoon\My Documents\2010_Config.xml")
        Dim nav As Xml.XPath.XPathNavigator = document.CreateNavigator

        Dim xpath As String = String.Format("//Chart[@ChartName='{0}']/{1}", chartName, elementName)

        Dim myString As String

        myString = document.SelectSingleNode(xpath).InnerXml

        document.SelectSingleNode(xpath).InnerXml = "0.6"

        myString = document.SelectSingleNode(xpath).InnerXml

my XML

VB.NET:
<?xml version="1.0"?> 
	<Excel>
		<Foo>
			<Chart ChartName = "Operational Availability">
				<Location>2010_OperationalAvailability.xlsm</Location>
				<Goal>0.8</Goal>
			</Chart>
			<Chart ChartName = "Absenteeism">
				<Location>2010_Absenteeism.xlsm</Location>
				<Goal>0.5</Goal>
			</Chart>
			<Chart ChartName = "Financials">
				<Location>2010_Financials.xlsm</Location>
				<Goal>0.15</Goal>
			</Chart>
		</Foo>
	</Excel>
 
Just add this line after you set the InnerXml to 0.6.

VB.NET:
document.Save("C:\Documents and Settings\honeymoon\My Documents\2010_Config.xml")

Tried it on my system and it works fine.
 
newbee mistake

You're right Matt, it does work, my problem was that I had forgotten to close an XmlReader elsewhere in my code:eek:

Thanks for all your help again guys!

FYI: I tried to give you guys reputation points, but its not letting me.
 
Also note you don't have to query twice, SelectSingleNode returns a XmlNode object that you can use to read/write.
 
Back
Top