Solved

Writing to XML file in C# WPF

Posted on 2014-01-17
7
1,533 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

12 Experts available now in Live!

Get 1:1 Help Now