0

I got below XML in file "test.xml"

<MainDoc version="1.0" application="App2">
  <property name="AutoHiddenPanelCaptionShowMode">ShowForAllPanels</property>
  <property name="DockingOptions" isnull="true" iskey="true">
    <property name="DockPanelInTabContainerTabRegion">DockImmediately</property>
  </property>
  <property name="Panels" iskey="true" value="4">
    <property name="Item1" isnull="true" iskey="true">
      <property name="Text">ContainerABC</property>
      <property name="Options" isnull="true" iskey="true">
        <property name="AllowFloating">true</property>
      </property>
    </property>
    <property name="Item2" isnull="true" iskey="true">
      <property name="Text">ContainerXYZ</property>
    </property>
    <property name="Item3" isnull="true" iskey="true">
      <property name="Text">Container123</property>
    </property>
    <property name="Item4" isnull="true" iskey="true">
      <property name="Text">panelContainer1</property>
    </property>
  </property>
</MainDoc>

I want to change element content where it says "panelContainer1" to "Container456" above. How can I do that. I tried below but not sure how to get to that content and change it.

using System.Xml.Linq;

private void button1_Click(object sender, EventArgs e)
        {
            string xmlPath = @"C:\Downloads\test.xml";
            XDocument doc = XDocument.Load(xmlPath);
            var items = from item in doc.Descendants("property")
                        where item.Attribute("name").Value == "Item4"
                        select item;
            foreach (XElement itemElement in items)
            {
                //something here ?
            }
        }
1
  • 2
    itemElement.Value = "Container456";? Commented Aug 3, 2021 at 14:18

1 Answer 1

1

Everything is looking good. and what you need is itemElement.Value

XDocument doc = XDocument.Load(xmlPath);
var items = doc.Root.Descendants("property")
                    .Where(x => x.Attribute("name").Value == "Item4")
                    .Descendants()
                    .Where(x=> x.Attribute("name").Value == "Text");

foreach(var itemElement in items)
{
    itemElement.Value = "Container456";
}

doc.Save(xmlPath); 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Krishna, this is good. But what if there are more elements like <property name="Text">panelContainer1</property> and also property name="Visibility">Visible</property> then how to filter only to get "panelContainer1"?
@kami, I have updated my post to address the scenario. Please check
This is good. Many thanks. Only last thing that this change is not being made to "test.xml" file. How that can be done?
@kami, please check the updated post. you just need doc.save
Simple and great. Thanks again @Krishna

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.