[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Error Updating XML Element Value

Posted on 2013-11-13
3
Medium Priority
?
264 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 64

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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