?
Solved

Error Updating XML Element Value

Posted on 2013-11-13
3
Medium Priority
?
263 Views
Last Modified: 2013-11-18
I have the following function and XML sample to update a value in an XML document. I want to change the Value for any one of my many elements. strSetting is XML Element "name" and strValue is the XML Element "Value".

When I run this I get no matching records found, however the watch shows the variables are correctly set.

Ideas?


        public void xtWriter(string strSetting, string strValue)
        {
            XDocument doc = new XDocument();

            XElement elementItem = doc
                .Elements("Settings")
                .Elements("Setting")
                .Elements("Name")
                .Elements("Value")
                .Single(Name => Name.Value == strSetting)
                .Parent
                .Element("Value");
            elementItem.Value = strValue;
        }

Open in new window


<Settings>
  <Setting>
    <Name>Title</Name>
    <Value>TitleHere</Value>
  </Setting>
  <Setting>
    <Name>Description</Name>
    <Value>DescriptionHere</Value>
  </Setting>
</Settings>

Open in new window

0
Comment
Question by:LouisM1979
[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
  • 2
3 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39646842
Hi LouisM1979;

I am not sure what you are attempting to do with your function xtWriter. This is what I think you want. The function is passing in two strings the first strSetting is the name of the node called Name and the second strValue is the value to set in the Value node. Is this correct?

The function creates a XDocument and then query it without ever loading a existing document. Did you mean to open an existing and query that document?

You do not save anything in the function and just leave it which means all info is lost.

Please explaine what you want this function to do.
0
 

Accepted Solution

by:
LouisM1979 earned 0 total points
ID: 39646883
I got it figured out eventually..........

I just want to update an XML element. Here is what I have that works.

        public void xtWriter(string strSetting, string strValue)
        {
            XDocument doc = XDocument.Load("ConfigSettings.xml");
            XElement root = doc.Root;

            root.Elements("Setting").Where(e => e.Element("Name").Value.Equals(strSetting)).Select(e => e.Element("Value")).Single().SetValue(strValue);
            doc.Save("ConfigSettings.xml");
        } //  Closes xtWriter

Open in new window

0
 

Author Closing Comment

by:LouisM1979
ID: 39656023
working code.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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