Solved

Error Updating XML Element Value

Posted on 2013-11-13
3
254 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
  • 2
3 Comments
 
LVL 62

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2013 debugging 2 35
How to avoid saving tif files a 2nd time using VS2015 and C#? 1 28
Runtime Error 2 29
Greetings C# program 17 57
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

867 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

23 Experts available now in Live!

Get 1:1 Help Now