何时使用C#更改XML的Root元素中的值 [英] Ho w to change a value in Root element of XML using C#
本文介绍了何时使用C#更改XML的Root元素中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何编辑XML文件的根元素中的值
< 模型 名称 = Test.cre version = v2.0 unit = m count = 0 xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance >
这里Model是我要更新的根元素使用C#在根元素中的count属性。
如何使用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()); // 如果你想要一个属性
}
xmlDoc.Save( @ C:\ Users \KK \Desktop\Sample.xml< /跨度>);
解决方案
请参阅以下链接
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
这篇关于何时使用C#更改XML的Root元素中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文