Solved

Error Updating XML Element Value

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

690 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