Solved

Writing to XML file in C# WPF

Posted on 2014-01-17
7
1,570 Views
Last Modified: 2014-01-18
Hi I need help updating an xml file in C# for two scenarios.

1) When a button is clicked on
2) when the form (program) exits.

I need to update the author, title and publisher values when these two actions take place.

Right now, the objects on the WPF form is binded to the xml file.  Thanks in advance.

Here's the xaml file:

[/<Canvas Canvas.Left="6" Canvas.Top="172" DataContext="{StaticResource BookData}" Height="79" Width="422">
                                <TextBox Background="{x:Null}" BorderBrush="{x:Null}" Canvas.Left="110" Canvas.Top="28" FontSize="12" FontWeight="Normal" IsReadOnly="True" Name="textBox2" Text="{Binding XPath=Book/Author}" Width="50" />

                                <TextBox Background="{x:Null}" BorderBrush="{x:Null}" Canvas.Left="110" Canvas.Top="52" FontSize="12" FontWeight="Normal" IsReadOnly="True" Name="textBox3" Text="{Binding XPath=Book/Title}" Width="50" />
                            

Open in new window


XML FILE
<?xml version='1.0'?>
<Data>
 <Book>
<title>The Test </title>
<author>Mike Williams </author>
<publisher>Angel Random </publisher>
 </Book>
</Data>
0
Comment
Question by:TeknikDev
  • 4
  • 3
7 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39790681
Can you please show the complete XAML code and the code behind.

Thanks.
0
 

Author Comment

by:TeknikDev
ID: 39790762
 public void XMLUpdate(string val1, string val2, string val3)
        {

if(val1 == "1")
{
string xmlfilepath = @"C:\Users\tom\Desktop\Test.xml"; 
            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\\Users\tom\Desktop\Test.xml");
            XmlNode root = doc.DocumentElement;
   XmlNode myNode = root.SelectSingleNode("Book:Author");
                myNode.Value = "Thomas"; 

            doc.Save(@"C:\\Users\tom\Desktop\Test.xml");

}

Open in new window



This isn't working - I get an error
Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function at this part of the code: XmlNode myNode = root.SelectSingleNode("Book:Author");

The logic is if I click on a button and call this method, where it says 1 then update value in xml file.

The method call is XMLUpdate("1", "0", "0") so this means only value 1 gets updated which is the author.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39790859
Hi TeknikDev;

The error you were getting was because you was using an xmlns, XML Namespace, in your query where the XML document did NOT have one. I changed your query to look for the first child node author of the node Book. Please note that in XML capitalization matters and so Book/Author is not the same as Book/author. Please have a look at XPath examples to see how to use them.

public void XMLUpdate(string val1, string val2, string val3)
{
    if(val1 == "1")
    {
        string xmlfilepath = @"C:\Users\tom\Desktop\Test.xml"; 
        XmlDocument doc = new XmlDocument();
        doc.Load(xmlfilepath);
        XmlNode root = doc.DocumentElement;
        XmlNode myNode = root.SelectSingleNode(@"Book/author");
        myNode.InnerText = "Thomas";
        doc.Save(xmlfilepath);
    }
}

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:TeknikDev
ID: 39791196
I'm still getting the same error.
0
 

Author Closing Comment

by:TeknikDev
ID: 39791198
This worked! Thanks!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39791203
Did you change this @"Book:Author" to this @"Book/author" ?
Also does the actual XML that you are using is like the one you posted in the question?

By the way the solution I posted works on my machine.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39791208
OK
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now