Solved

Error Updating XML Element Value

Posted on 2013-11-13
3
251 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

15 Experts available now in Live!

Get 1:1 Help Now