Solved

Writing to XML file in C# WPF

Posted on 2014-01-17
7
1,646 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 63

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 63

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 63

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 63

Expert Comment

by:Fernando Soto
ID: 39791208
OK
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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