Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Writing to XML file in C# WPF

Posted on 2014-01-17
7
Medium Priority
?
1,806 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 64

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

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 64

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 64

Expert Comment

by:Fernando Soto
ID: 39791208
OK
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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