C# 如何使用C在XML中编辑特定的子字符串
例如,我有一个xml文件,如下所示C# 如何使用C在XML中编辑特定的子字符串,c#,xml,C#,Xml,例如,我有一个xml文件,如下所示 <file> <add key="1" val="great.me"/> <add key="2" val="notSoGreat"/> <add key="3" val="lessGreat.me/yey" /> <add key="4" val="soGreat/yey" /> </file> 我想取代那些。我要。太棒了 例: 到 及 例:试试这个 string oldTex
<file>
<add key="1" val="great.me"/>
<add key="2" val="notSoGreat"/>
<add key="3" val="lessGreat.me/yey" />
<add key="4" val="soGreat/yey" />
</file>
我想取代那些。我要。太棒了
例:
到
及
例:试试这个
string oldText = File.ReadAllText(filePath);
string newText = oldText.Replace("me", "awesome");
File.WriteAllText(filePath, newText, Encoding.UTF8);
xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
请参考如何提问-你试过什么?代码中您面临的具体问题是什么?你做了些什么来尝试解决这些问题阻止你们!!
//load xml from file
XmlDocument doc = new XmlDocument();
doc.Load(@"E:\test1.xml");
// get a list of nodes -
XmlNodeList allnodes = doc.SelectNodes("/file/add");
// loop through all nodes
foreach (XmlNode node in allnodes)
{
// get "value"
XmlAttribute attrValue= node.Attributes["value"];
attrValue.value = "something";// use replace here
}
// save new xml or you can overwrite
doc.Save(@"E:\test1New.xml");