Results 1 to 7 of 7

Thread: Can I use XPath to write to an xml file?

  1. #1
    ucchris77 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jul 2010
    Posts
    6
    Reputation
    0

    Can I use XPath to write to an xml file?

    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")

  2. #2
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    572
    Rather than SetValue use InnerXml.

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,225
    Reputation
    2370
    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.

  4. #4
    ucchris77 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jul 2010
    Posts
    6
    Reputation
    0

    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.

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

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

    Code:
    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

    Code:
    <?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>

  5. #5
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    572
    Just add this line after you set the InnerXml to 0.6.

    Code:
    document.Save("C:\Documents and Settings\honeymoon\My Documents\2010_Config.xml")
    Tried it on my system and it works fine.

  6. #6
    ucchris77 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jul 2010
    Posts
    6
    Reputation
    0

    newbee mistake

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

    Thanks for all your help again guys!

    FYI: I tried to give you guys reputation points, but its not letting me.

  7. #7
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,225
    Reputation
    2370
    Also note you don't have to query twice, SelectSingleNode returns a XmlNode object that you can use to read/write.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking