且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

何时使用C#更改XML的Root元素中的值

更新时间:2023-01-21 15:43:44

请参阅以下链接

How to edit a value in root element of XML file

<Model name="Test.cre" version="v2.0" unit="m" count="0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">




Here Model is the root element I want to update the count attribute in root element using C#.

How to do this using C#



           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.Load(@"C:\Users\KK\Desktop\Sample.xml");

XmlElement node1 = xmlDoc.SelectSingleNode("/root") as XmlElement;
           if (node1 != null)
           {

               node1.SetAttribute("count", nodeCount.ToString()); // if you want an attribute

           }

           xmlDoc.Save(@"C:\Users\KK\Desktop\Sample.xml");

Please refer the following Link