Linq to Xml - Update/Alter the Nodes of an Xml Document

How to update XML attributes using Linq to XML in C#

You can accomplish the update with your method by adding one more inner loop:

foreach (XmlNode checkedNode in node.ChildNodes)
{
checkedNode.Attributes["Checked"].Value = "true";
}

If you want to use Linq, you should switch to XDocument:

var doc = XDocument.Parse(xml);     //xml is a string, can be returned from a function, 
//built dynamically, etc.

var nodesToUpdate = doc.Descendants("Node")
.Where(n => n.Attributes("Checked").FirstOrDefault() != null);

foreach (var node in nodesToUpdate)
{
//TODO: check update conditions, etc.

node.Attribute("Checked").Value = "true";
}

LINQ to XML - Update and save a node to XML file

What this does is that it overwrittes the original XML file with only this document node. How can I update the document node in the original file?

You need to save the whole document, instead of just the changed element. That will rewrite the whole file, of course, but there's no simple way round that.

xmlDocuments.Save(filePath);

How to update all instances of an element in XDocument?

Linq is a query language, so you can't directly use it to modify the value, but you can easily select all the Resource elements in the document with it and iterate/change them.

For example:

// or load from xml, however you have it
var xDoc = XDocument.Load(@"c:\temp\myxml.xml");
// iterate every Resource element
foreach (XElement element in xDoc.Descendants("Resource"))
element.Value = "Hello, world";

That will pick out every Resource element in the XML regardless of where it is in the hierarchy, which in your case, is what you need. If you needed to target it more specifically, you could either use an XPath expression or further Linq calls such as Element() which work on a single level of the hierarchy.

How to update node values in XML using C#

Got it!

public static void ReplaceCode()
{
var root = new XmlDocument();
root.Load(@"C:\data.xml");

foreach (XmlNode e in root.GetElementsByTagName("alternateID"))
{
if (e.Attributes["code"].Value.Equals("ALT"))
{
e.FirstChild.Value = "00000000"; // FirstChild because the inner node is actually the inner text, yeah XmlNode is weird.
break;
}
}
root.Save(@"C:\data.xml");
}

Ask me anything about it and I can clarify. :)

C# : Modify a xml node

Try this:

xml.SelectSingleNode("//reminder/Title").InnerText = "NewValue";

Your foreach line is simply looping through a list of elements called "reminders", not it's child nodes.

Take a look at this xpath tutorial for more information:

http://www.w3schools.com/xpath/xpath_intro.asp

Updating XML nodes from an object list in C#

Your XML doesn't have <count> element. If you meant to update <stock> element value, the first parameter of SetElementValue() should be "stock" :

if (node.Element("recordNumber").Value == Convert.ToString(item.Id))
node.SetElementValue("stock", Convert.ToString(item.count));

One possible way using LINQ join to create anonymous type that pair <product> element with the corresponding item from stockArray :

var list = from product in doc.Element("inventory").Elements("product")
join item in stockArray on (int)product.Element("recordNumber") equals item.id
select new {product = product, item = item};
foreach (var joinedProduct in list)
{
joinedProduct.product.SetElementValue("stock", joinedProduct.item.count);
}

How to update xml file without using Linq?

Your Code is Correct here is a screen shot of your debugged code with same xml and mentioned code, check you are passing student.name with correct value and case sensitive.

enter image description here

How to Update a Specific Child Node Value of XML Document in C#

To make InnerText property available in auto complete just change your for using the correct type of your node variable

foreach (XmlElement node in parameterNode)

To remove the node in your if just select the parent of you node and remove the current node from the parent:

if(node.InnerText == parameterValue)
{
var parentNode = node.ParentNode;
parentNode.RemoveChild(node);
}

You can see an example of your full conde in this link:

https://dotnetfiddle.net/2Fe2ss



Related Topics



Leave a reply



Submit